Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
chridd
Has a vermicelli title
Posts: 846
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Thu Sep 08, 2011 6:04 am UTC

EvanED wrote:Can't you drag tabs between windows in Chrome? (I don't use it on Windows and can't test right now.)
Yes you can. (At least on Mac, I assume it's also true on Windows.) Though Pesto mentioned two maximized windows, which would make that more difficult (since the windows aren't both visible at the same time, assuming only one monitor is involved).
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she · Forum game scores
mittfh wrote:I wish this post was very quotable...

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Thu Sep 08, 2011 7:05 am UTC

Yeah, when dealing with 2 maximized windows, it's annoying to have to non-maximize them (how is it that the opposite of "maximize" is NOT "minimize"?), select the tab, drag it to the other window (being careful not to release the mouse, or risking a third window, further obscuring the target one), and release there, and then maximizing them both...
In Minecraft, I use the username Rirez.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Sep 08, 2011 8:31 am UTC

Steax wrote:Yeah, when dealing with 2 maximized windows, it's annoying to have to non-maximize them, select the tab, drag it to the other window (being careful not to release the mouse, or risking a third window, further obscuring the target one), and release there, and then maximizing them both...

Depending on your WM, you may be able to drag your tab to the equivalent of the taskbar, hover over the icon for the window you want, and wait a second. Not saying that's necessarily a better way (especially 'cause I think most browsers will reload the page anyway, not sure about chrome obviously).

(how is it that the opposite of "maximize" is NOT "minimize"?)

How is "maximum" not the opposite of "minimum" if you have more than two elements? :-)

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Thu Sep 08, 2011 9:47 am UTC

I uh... I...

I'll be over there to regain my common sense.
In Minecraft, I use the username Rirez.

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Sep 08, 2011 1:43 pm UTC

Incidentally, if the tooltips are to be believed, the opposites of "Maximise" and "Minimise" are both "Restore". The more you know.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Thu Sep 08, 2011 5:16 pm UTC

EvanED wrote:
Steax wrote:Yeah, when dealing with 2 maximized windows, it's annoying to have to non-maximize them, select the tab, drag it to the other window (being careful not to release the mouse, or risking a third window, further obscuring the target one), and release there, and then maximizing them both...

Depending on your WM, you may be able to drag your tab to the equivalent of the taskbar, hover over the icon for the window you want, and wait a second. Not saying that's necessarily a better way (especially 'cause I think most browsers will reload the page anyway, not sure about chrome obviously).

Chrome definitely does not reload the page when you drag tabs. I don't think Firefox does either.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Anonymously Famous
Posts: 242
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: Coding: Fleeting Thoughts

Postby Anonymously Famous » Thu Sep 08, 2011 7:14 pm UTC

I can confirm that FF does not reload when you drag tabs. And in Windows, you can indeed drag to the Window you want, wait for that window to appear, and place the tab where you want it. I also just dragged the tab to somewhere in the middle of the screen and Alt+tabbed to the other FF window to do the same thing.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Sep 08, 2011 9:47 pm UTC

Huh, guess I'm wrong. I haven't done it in a while, so maybe it used to and they changed it. Or maybe I'm just completely wrong and it never did, and I confused re-rendering with reloading.

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Thu Sep 08, 2011 9:54 pm UTC

I have this memory of Firefox doing a reload when relocating a tab into a new window.

Anonymously Famous
Posts: 242
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: Coding: Fleeting Thoughts

Postby Anonymously Famous » Thu Sep 08, 2011 10:19 pm UTC

It may have at one point, but I did it in the middle of replying, and my reply text stayed untouched, which indicates to me that it doesn't reload (though I just did it now and selected text was unselected). You can even drag it to directly to your task bar and it will open a new window.

User avatar
Macbi
Posts: 941
Joined: Mon Apr 09, 2007 8:32 am UTC
Location: UKvia

Re: Coding: Fleeting Thoughts

Postby Macbi » Thu Sep 08, 2011 11:49 pm UTC

Ubik wrote:I have this memory of Firefox doing a reload when relocating a tab into a new window.


Me too, maybe an older version? It works fine now.
    Indigo is a lie.
    Which idiot decided that websites can't go within 4cm of the edge of the screen?
    There should be a null word, for the question "Is anybody there?" and to see if microphones are on.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Fri Sep 09, 2011 4:21 am UTC

WHY, IPAD, WHY?

It won't let me use the newer -webkit-linear-gradient syntax, it wants the old one. NO WAY AM I CONVERTING ALL THOSE GRADIENTS FOR YOU. NO.

Sorry, you'll have to live with your dumb version until apple decides to upgrade you. iOS 5, please?

Also, it's annoying when dealing with widths. I'll have to make a media query for its width... NOOOOOOOOOOOOO
Last edited by Steax on Fri Sep 09, 2011 4:26 am UTC, edited 1 time in total.
In Minecraft, I use the username Rirez.

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Fri Sep 09, 2011 4:24 am UTC

Yeah, old versions of Firefox, when you dragged a tab between windows, would just close the tab in the source window and create a new tab in the destination window with the same URL. I'm not sure when it switched to doing it properly... I think Firefox 4, maybe?

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Fri Sep 09, 2011 4:49 am UTC

Okay, today I learned: If you want to a website to easily port over to tablets, do not use a fixed width larger than 900.

Or build with media queries in mind from the first place.
In Minecraft, I use the username Rirez.

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Fri Sep 09, 2011 7:13 am UTC

Today's comic made me think that there could be a simple program which would not require any kind registration or rely on any central server - something which would possibly make this more easier to do with not so tech-savvy people.

A file browser contextual menu could have a "Send this file" item, which would then ask for recipient's e-mail address and then send mail with a link and start up an HTTP daemon in a high port to serve the file. Recipient would only have to open the link and save the file. Too bad NAT will make this infeasible, but maybe when IPv6 actually comes?

I also started to think if a bit more complex (=nasty) system with UDP data transfer could work, maybe the e-mail headers could be used to find out the address of the sender and to punch a hole into the device doing the NAT at the recipient's end...

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Fri Sep 09, 2011 7:23 am UTC

It could be a downloadable program for the sender in which the file is tunneled through a proxy server. As long as the senders computer opens the connection to the proxy, firewalls won't be a problem.
Summum ius, summa iniuria.

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Fri Sep 09, 2011 7:44 am UTC

The downside with that would be the reliance to the proxy service. Keeping up one would require money which would eventually mean loss of simplicity (either by adding ads or by requiring money to use the system).

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Fri Sep 09, 2011 7:58 am UTC

Yeah, you're always going to have one of two problems:

Difficulty of use with firewalls.
Reliance on a central server.

Take your pick. With the solution I propose, the server is very lightweight except for bandwidth usage, so hardware would be cheap, it's just bandwidth. You might be able to put it on a cloud for a reasonable price and support it with donations.
Summum ius, summa iniuria.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Fri Sep 09, 2011 10:04 am UTC

Thesh wrote:It could be a downloadable program for the sender in which the file is tunneled through a proxy server.

Registration > Downloadable program. You're suggesting avoiding a mild annoyance for the sender by telling the receiver to do something far more annoying and dangerous.

If someone sent me a link that required me to download some program just for the purpose of downloading what I actually want, I'd probably tell them to do it better and ignore it. Only if said program was pretty-well established and open source, or what I wanted was really important (do this for a job or something) would I do it.

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Fri Sep 09, 2011 11:48 am UTC

EvanED wrote:
Thesh wrote:It could be a downloadable program for the sender in which the file is tunneled through a proxy server.

Registration > Downloadable program. You're suggesting avoiding a mild annoyance for the sender by telling the receiver to do something far more annoying and dangerous.

Huh? In the bit you just quoted Thesh says the sender downloads a tunneling file server program; the receiver doesn't download anything, except for the actual file they are wanting to receive.

I think the above scheme sounds like a good idea for sending the occasional file, but if someone needs to send large files regularly it probably makes more sense for them to set up a proper FTP server.

Another option I've seen discussed for occasional transfers of large files is to use remote desktop &/or remote backup technology, although the security aspects involved mean that you probably wouldn't want to do that with someone you don't know and trust. :)

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Fri Sep 09, 2011 12:26 pm UTC

Disclaimer: I'm not the kind of guy who deals with low-level programming stuff, because I meddle around enough in design, and I'd be doomed to explore further in that direction.

So back when I was a heavy Sphere (javascript-based game engine) user, I was often warned about using functions too much - they cause overhead. Now when I look at, say, PHP with its batrillions of functions, I can't help but think of the ultimate horrors that overhead must cause there.

Yet apparently it's fine, and frameworks toss in hundreds of helper functions all the time. Is it that these functions don't cause overhead, less of it, or the overhead is less of a problem overall?
In Minecraft, I use the username Rirez.

User avatar
Robert'); DROP TABLE *;
Posts: 730
Joined: Mon Sep 08, 2008 6:46 pm UTC
Location: in ur fieldz

Re: Coding: Fleeting Thoughts

Postby Robert'); DROP TABLE *; » Fri Sep 09, 2011 1:31 pm UTC

I would say that unless you were writing for an embedded controller or something, whoever told you that doesn't know what they're talking about. In any compiled language, functions would take up one or two words each, out of thousands (for larger programs, millions) the actual instructions occupy. Something similar probably applies to interpreters.
Last edited by Robert'); DROP TABLE *; on Fri Sep 09, 2011 3:11 pm UTC, edited 1 time in total.
...And that is how we know the Earth to be banana-shaped.

Anonymously Famous
Posts: 242
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: Coding: Fleeting Thoughts

Postby Anonymously Famous » Fri Sep 09, 2011 2:50 pm UTC

phlip wrote:Yeah, old versions of Firefox, when you dragged a tab between windows, would just close the tab in the source window and create a new tab in the destination window with the same URL. I'm not sure when it switched to doing it properly... I think Firefox 4, maybe?

The browser I tested it on was 3.6.(something).

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Fri Sep 09, 2011 3:22 pm UTC

Robert'); DROP TABLE *; wrote:I would say that unless you were writing for an embedded controller or something, whoever told you that doesn't know what they're talking about. In any compiled language, functions would take up one or two words each, out of thousands (for larger programs, millions) the actual instructions occupy. Something similar probably applies to interpreters.


Yeah, though I thought it might have to do with javascript being an interpreted language and having very flexible functions.
In Minecraft, I use the username Rirez.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Fri Sep 09, 2011 3:28 pm UTC

Steax wrote:Disclaimer: I'm not the kind of guy who deals with low-level programming stuff, because I meddle around enough in design, and I'd be doomed to explore further in that direction.

So back when I was a heavy Sphere (javascript-based game engine) user, I was often warned about using functions too much - they cause overhead. Now when I look at, say, PHP with its batrillions of functions, I can't help but think of the ultimate horrors that overhead must cause there.

Yet apparently it's fine, and frameworks toss in hundreds of helper functions all the time. Is it that these functions don't cause overhead, less of it, or the overhead is less of a problem overall?

Functions are not a problem. Using a function is a *very very slight* performance loss over inlining the code *sometimes*, but unless you're willing to spend time profiling and commit to redoing your performance measurements over time, DO NOT TRY AND CARE ABOUT IT. I warn you for your own safety.

In PHP, inexplicably, functions are pretty expensive. I have no idea why, except that the PHP devs aren't that good. Still, though, that just means that relying on recursive functions or mapping might be a bit slower than doing the same thing iteratively. It's almost certainly nothing you should care about.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Fri Sep 09, 2011 3:52 pm UTC

Steax wrote:Disclaimer: I'm not the kind of guy who deals with low-level programming stuff, because I meddle around enough in design, and I'd be doomed to explore further in that direction.

So back when I was a heavy Sphere (javascript-based game engine) user, I was often warned about using functions too much - they cause overhead. Now when I look at, say, PHP with its batrillions of functions, I can't help but think of the ultimate horrors that overhead must cause there.

Yet apparently it's fine, and frameworks toss in hundreds of helper functions all the time. Is it that these functions don't cause overhead, less of it, or the overhead is less of a problem overall?


This topic may be worth putting into its own thread, Steax.

There are two kinds of overhead associated with functions. There is the overhead of actually calling the function: passing parameters & the return value, and setting up the local variables that the function uses. That obviously takes up space and time compared to inline code.Then there is the overhead of loading the actual function code: in the worst case scenario you may have to pull in a whole library of functions to get access to a single function in that library. Fortunately that sort of thing is prettalmost unheard-of these days. But the actually details of how that side of things gets handled depends very much on how the language works and how it interacts with its operating environment.

It's quite possible that many of the apparent inefficiencies of using functions get magicked away during compilation, and that can be true even for interpreted programs, since it's common these days for interpreters to perform some kind of compilation pass on programs before they interpret them. Another trick that interpreters can do is to have standard functions built into the interpreter code itself so that they become almost as efficient as any other command of the language.

I must confess that although I often write in JavaScript I have no idea of how it works at the low level, but I do know that modern JS interpreters tend to use JIT techniques to make things efficient. Standard functions in JS are all actually object methods, and I get the impression that the are handled fairly efficiently.


FWIW, I'm no stranger to low-level programming - my 3rd language was IBM 360 assembler, which I learned 5 years before I'd even heard of C, and I've been using C for just over 30 years. :)


Edit: Ninja'd by Xanthir. Oh well. :) But I certainly agree with what he says, and I'll add that the First Rule of Premature Optimization is: Don't!

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Fri Sep 09, 2011 4:09 pm UTC

Thanks guys, I've been wondering about that. So overall, I guess this is the sort of thing which might slow down my code, yet fixing that little neighboring loop over there might do me better than optimizing my whole application by redoing functions? As in, even less than the category of using x *= 0.5 over x = x/2, and using while loops over for?

I know there' a huge category of crap which miiiiiight increase application responsiveness by half a millisecond during peak load, but they're probably not worth anyone's time or effort.
In Minecraft, I use the username Rirez.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Fri Sep 09, 2011 4:19 pm UTC

No profiling software available?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Fri Sep 09, 2011 4:22 pm UTC

Yep.

As Xanthir said: DO NOT TRY AND CARE ABOUT IT. I warn you for your own safety sanity.

And it's useless trying to make generalizations about this stuff, it really depends on what language you're talking about, and if you're talking about an interpreted language, how the particular interpreter is implemented. As for JavaScript, it's probably pointless trying to make your code run as fast as possible in your browser, since another JS engine / browser combination will behave differently.

Also, read this:
Premature Optimization wrote:In DonaldKnuth's paper "StructuredProgrammingWithGoToStatements", he wrote: "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%."

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sat Sep 10, 2011 5:17 pm UTC

Fuck finite state automata parsers. They can all go die in a horrible fire. If your language has a reasonably consistent grammar*, it's easy--almost trivial--to write a parser that actually makes sense.

* i.e. no stuff like dangling elses and C99/C++ style for loops.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

userxp
Posts: 436
Joined: Thu Jul 09, 2009 12:40 pm UTC

Re: Coding: Fleeting Thoughts

Postby userxp » Mon Sep 12, 2011 11:53 am UTC

Maybe it's just me, but doesn't the canvas element go against basic HTML principles? I mean, HTML is generally a very high-level language, and canvas is basically low-level display access for Javascript. It seems to me that SVG is a much more logical choice: it uses the standard DOM model and leaves the rendering to the browser. Of course it's less efficient, but that's partly because web browsers have focused their optimization efforts into canvas instead of SVG. Besides, if you want efficiency, use Java applets or something, not HTML.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Mon Sep 12, 2011 1:50 pm UTC

userxp wrote:Maybe it's just me, but doesn't the canvas element go against basic HTML principles? I mean, HTML is generally a very high-level language, and canvas is basically low-level display access for Javascript. It seems to me that SVG is a much more logical choice: it uses the standard DOM model and leaves the rendering to the browser. Of course it's less efficient, but that's partly because web browsers have focused their optimization efforts into canvas instead of SVG. Besides, if you want efficiency, use Java applets or something, not HTML.


Which HTML principle do you mean? I'd think that SVG could be a bigger offender in terms of semantics, because you end up with a lot of abstract elements instead of "here's a canvas with some picture on it".
In Minecraft, I use the username Rirez.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Mon Sep 12, 2011 5:35 pm UTC

Many of the things done today with <canvas> would be better done with SVG, if only SVG's programming interface (the DOM) wasn't so horrific. We're hoping to clean up a whole bunch of that with SVG2. Using SVG lets you conveniently forget things after you no longer need to care about them (like the size/shape of an element, if you only need to worry about its position later on), and makes some things much easier, like hit-testing complex shapes.

However, <canvas> lets you do a bunch of things that SVG can't do, like direct pixel manipulation.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Mon Sep 12, 2011 6:00 pm UTC

Xanthir wrote:However, <canvas> lets you do a bunch of things that SVG can't do, like direct pixel manipulation.

I don't know very much about SVG, but from what I've seen it seems ok, and although the XML approach makes it a little bit bulky it also keeps things neat & tidy. I mostly use Firefox 4.0.1 as my browser, and it doesn't seem to like compressed SVG, which is a bit annoying.

I've only been programming with <canvas> for a few months, but I like it, despite a few minor annoyances. Quite a few of the canvas operations remind me of PostScript, but I guess that's intentional. I'm quite impressed with the speed that Firefox 4.0.1 runs JavaScript and renders <canvas> stuff. For example, here's an animated globe generating program I wrote a month or so ago. Any constructive criticism is welcome!

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Globe</title>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<style type="text/css">
body{ background-color: #404040 }
h4, hr, a {color: #fa8 }
p { color: #ff8 }
p.centered{ text-align: center }
span {font-size: large; color: #fa8 }
th, h3 { text-align: center; font-size: large; color: #fa8 }
input { width: 90px; }
table { color: #ff8;  border: solid;  border-color: #fa8 }
</style>

<script type="text/javascript">
var srcMap, mapname =
    //'images/EarthColour_2048.jpg',
    'http://maps.jpl.nasa.gov/pix/jup0vss1.jpg',
    srcpix, octx, ocanvasData, pixels,
    iwidth, iheight, owidth, oheight,
    rad = 180, iscale = 1.25,
    blur = 0.65, gamma = 0.75,
    delay = 50, delta = 0, ddelta = 1, go = false, timeout;

function byId(id) { return document.getElementById(id); }

//Create a canvas element
function make_canvas(w, h)
{
    var c = document.createElement('canvas');

    c.setAttribute('width', w);
    c.setAttribute('height', h);
    return c;
}

//Do Gaussian blurring from src image / canvas into canvas context ctx.
// Mixing ratio: 0 <= r <= 1. 0=Not blurred, 1 = fully blurred
function drawBlurredImage(ctx, src, r)
{
    ctx.globalAlpha = 1 - 12*r/16;
    ctx.drawImage(src, 0, 0);
    if (r == 0)
        return;

    ctx.globalAlpha = 2*r/16;
    ctx.drawImage(src, -1, 0); ctx.drawImage(src, 1, 0);
    ctx.drawImage(src, 0, -1); ctx.drawImage(src, 0, 1);

    ctx.globalAlpha = 1*r/16;
    ctx.drawImage(src, -1, -1); ctx.drawImage(src, 1, -1);
    ctx.drawImage(src, -1, 1); ctx.drawImage(src, 1, 1);
}

// Get canvas pixel data from context ctx
function get_pixels(ctx, ox, oy, w, h)
{
    try
    {
        try
        {
            //Attempt to read canvas data
            var imgd = ctx.getImageData(ox, oy, w, h);
        }
        catch (e)
        {
            //Ask user to give us permission to read canvas data
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            var imgd = ctx.getImageData(ox, oy, w, h);
        }
    }
    catch (e)
    {
        var msg = "Unable to access Canvas image data: " + e;

        alert(msg);
        throw new Error(msg);
    }
    return imgd;
}

function init()
{
    set_inputs();
    init_globe();
}

function init_globe()
{
    owidth = 2 * rad; oheight = owidth;
    iheight = Math.floor(iscale * oheight);
    iwidth = 2 * iheight;

    init_ocanvas();
    init_image();
}

function init_ocanvas()
{
    var ocanvas;

    //Set up display canvas
    ocanvas = document.getElementById('outcanvas');
    if (!ocanvas.getContext)
        return alert("Sorry, I can't set up the canvas!");

    ocanvas.width = owidth;
    ocanvas.height = oheight;

    octx = ocanvas.getContext('2d');
    octx.fillStyle = "rgba(64,64,64, 255)";
    octx.fillRect(0, 0, owidth, oheight);
    ocanvasData = get_pixels(octx, 0, 0, owidth, oheight);
    pixels = ocanvasData.data;
}

//Load source map image & wait for image to finish loading before proceeding
function init_image()
{
    //Load globe lat-long map
    srcMap = document.createElement('img');
    srcMap.src = mapname;
    srcMap.onload = init_map;
}

function init_map()
{
    var i, tmpCanvas, tmpCtx, srcCanvas, srcCtx;
   
    //var sc = Math.max(iwidth / srcMap.width, iheight / srcMap.height);

    //Scale source image to final size
    tmpCanvas = make_canvas(iwidth, iheight);
    tmpCtx = tmpCanvas.getContext('2d');
    tmpCtx.scale(iwidth / srcMap.width, iheight / srcMap.height);
    tmpCtx.drawImage(srcMap, 0, 0);

    //Create the source canvas
    srcCanvas = make_canvas(iwidth, iheight);
    srcCtx = srcCanvas.getContext('2d');

    //Do Gaussian blurring on scaled source image
    drawBlurredImage(srcCtx, tmpCanvas, blur)

    //Get pixel data from scaled blurred source map
    srcpix = get_pixels(srcCtx, 0, 0, iwidth, iheight).data;

    if (gamma != 1.0)
    {
        function do_gamma(p) { return Math.floor(255*Math.pow(p/255, gamma)); }

        //On each pixel, do gamma correction
        for (i=0; i<srcpix.length; i+=4)
        {
            srcpix[i] = do_gamma(srcpix[i]);
            srcpix[i+1] = do_gamma(srcpix[i+1]);
            srcpix[i+2] = do_gamma(srcpix[i+2]);
        }
    }
    draw();
}

//Project from lat-long form to orthographic
function draw()
{
    var i, j, ko, ki, u, v, r, pi = Math.PI,
    y, oy = oheight / 2, oy2 = oy * oy, ox = owidth / 2;

    for (j=0; j<oheight; j++)
    {
        y = j - oy;
        v = Math.floor(iheight * (0.5 + Math.asin(y / oy) / pi));
        r = Math.floor((Math.sqrt(oy2 - y*y)));
        for (i=1-r; i<r-1; i++)
        {
            u = Math.floor(iwidth * (2 - 0.5 * Math.acos(i/r) / pi) - delta) % iwidth;
            ki = 4 * (u + iwidth * v)
            ko = 4 * (ox + i + owidth * j);
            pixels[ko++] = srcpix[ki++];
            pixels[ko++] = srcpix[ki++];
            //pixels[ko++] = srcpix[ki++];
            pixels[ko] = srcpix[ki];
        }
    }
    octx.putImageData(ocanvasData, 0, 0);
    delta = (delta + iwidth + ddelta) % iwidth;
}

function timer()
{
    if (go)
    {
        draw();
        timeout = window.setTimeout("timer()", delay);
    }
}

//Toggle timer
function pause()
{
    if(go = !go)
    {
        timer();
        byId('ipause').value = "Pause";
    }
    else
    {
        window.clearTimeout(timeout);
        byId('ipause').value = "\xA0Play\xA0\xA0";
    }
}

//Set input value fields from Javascript global vars
function set_inputs()
{
    byId('ipause').value = "\xA0Play\xA0\xA0";
    byId('imap').value = mapname;
    byId('irad').value = rad;
    byId('iiscale').value = iscale;
    byId('iblur').value = blur;
    byId('igamma').value = gamma;
    byId('idelay').value = delay;
    byId('iddelta').value = ddelta;
}

//Update Javascript global vars from input value fields
function update()
{
    //Pause while changing vars
    go = true;
    pause();

    mapname = byId('imap').value;
    rad = byId('irad').value - 0;
    iscale = byId('iiscale').value - 0;
    blur = byId('iblur').value - 0;
    gamma = byId('igamma').value - 0;
    delay = byId('idelay').value - 0;
    ddelta = byId('iddelta').value - 0;

    delta = 0;
    init_globe();
    pause();
}

</script>
</head>
<body onload="init();">
<p class="centered">
<canvas id="outcanvas">
If you can read this, your browser does not support the HTML5 Canvas.
</canvas>

<p>
<input type="button" id="ipause" onclick="pause();">

<table>
<th colspan="4">Parameters</th>
<tr> <td> Map    <td colspan="3"> <input type="text" id="imap" style="width: 260px"> </tr>
<tr> <td> Radius <td> <input type="text" id="irad">
 <td> Scale  <td> <input type="text" id="iiscale"> </tr>
<tr> <td> Blur   <td> <input type="text" id="iblur">
 <td> Gamma  <td> <input type="text" id="igamma"> </tr>
<tr> <td> Delay  <td> <input type="text" id="idelay" onchange="delay=this.value-0">
 <td> Delta  <td> <input type="text" id="iddelta" onchange="ddelta=this.value-0"> </tr>

<tr> <td colspan="2"> <input type="button" id="update" value="Update" onclick="update();"> </tr>
</table>

<hr>

<h3>Globe</h3>
<h4>by PM 2Ring</h4>
<p>
This JavaScript / HTML program creates animated globe images from planet and moon map images in equirectangular format. This is the usual format that NASA uses for its map images: a full planet (or moon) image in this format is twice as wide as it is tall. The program can read image files from an Internet URL, or from your hard drive. The map image data is scaled, blurred and gamma corrected before being orthogonally projected. The projection function uses no interpolation, so it runs reasonably fast, but the downside is that the resulting image is a bit "sparkly", especially on the left &amp; right edges. This can be alleviated by playing with the scale of map image data, and by the use of blurring.<br><br>

For security reasons, your browser may not allow scripts to access the pixel data of images on the local hard drive, or on domains different from the domain where the script is hosted. If that is the case, your browser will pop up a dialog asking you to give it explicit permission. The original version of this script doesn't do anything dodgy; obviously I can make no claims about any modified scripts that are built from it...<br><br>

The image processing &amp; display utilizes the HTML5 Canvas element, so it may not work on older browsers.<br>

<h4>Parameters</h4>
<p>
<span>Map:</span> The URL or file name of the equirectangular map image file, which should be a JPEG, PNG, SVG, or GIF file (or any other format your browser can display directly). If you wish to use a local file, you can give its name relative to the directory you are running this script from, or you can give the file name in absolute form by specifying the name as a file:// URL. The default value for this parameter is a JPL / NASA URL for a map of Jupiter; there are various other maps in the <a href="http://maps.jpl.nasa.gov/pix/">same directory</a>.<br><br>

<span>Radius:</span> The radius (in pixels) of the globe image.<br><br>

<span>Scale:</span> This controls the scaled size of the source map, relative to the globe image: a scale of 1 makes the map the same height as the globe. A scale around 1 gives the best results. If the scale is too large, the globe will be too sparkly; if it's too small, the globe will get pixelated. The scale also controls the speed that the globe turns: the larger the scale, the slower the rotation.<br><br>

<span>Blur:</span> How much blurring to apply. 0 = no blurring, 1 = maximum blurring.<br><br>

<span>Gamma:</span> Gamma correction factor. This affects the brightness of the image. Gamma values less than 1 make it brighter, greater than 1 make it darker. Gamma must be greater than zero.<br><br>

<span>Delay:</span> The time delay between frames, in milliseconds, not counting processing time. The default of 50 should be adequate. If you try to speed it up or slow it down too much the resulting animation may be jerky.<br><br>

When you change the parameters to new values, hit the Update button to tell the program to regenerate the data with the new values. The delay parameter can be modified without using the Update button (just hit enter after changing the delay value), but changes to the other parameters require the map data to be recalculated; the Update button allows you to change multiple parameters without the calculations being redone for every single change.<br><br>

To keep this program as simple and compact as possible, there is NO error checking performed on any parameter values entered, so if you enter weird values outside the acceptable range, expect weird results. :)<br>
<hr>
</body>
</html>

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Mon Sep 12, 2011 6:15 pm UTC

Honestly, I think we need to choose another language to replace JavaScript. It is by far the worst language I've ever worked with. I don't know why we stayed with it for so long. Now with canvas, we are just going to be using it more and more. Blegh.
Summum ius, summa iniuria.

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Mon Sep 12, 2011 6:38 pm UTC

Thesh wrote:Honestly, I think we need to choose another language to replace JavaScript.

Maybe. If so, I vote for Python.

Thesh wrote:It is by far the worst language I've ever worked with. I don't know why we stayed with it for so long. Now with canvas, we are just going to be using it more and more. Blegh.

Coming from several decades of using C I first thought of JavaScript as merely a handy scripting language and I felt that some of its features (and some of the online tutorials) tended to encourage sloppy programming habits. But I've gradually come to believe that beneath JavaScript's lumpy exterior there is a beautiful and powerful language waiting to be liberated. I certainly won't claim to be a JavaScript expert, but I think part of the blame for bad JavaScript programs is that if you're writing a JavaScript & are having problems it's very easy to find band-aid solutions on the Net, and some of those "solutions" are not very good code, and they may be quite old. So you end up with lots of cargo-cult code that's patched together without much planning or coherent structure.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Mon Sep 12, 2011 7:18 pm UTC

PM 2Ring wrote:
Thesh wrote:Honestly, I think we need to choose another language to replace JavaScript.

Maybe. If so, I vote for Python.


I would be fine with that, but being that many businesses like to create javascript files with everything on one line, I'm not sure it would be able to end the reign of JavaScript which is going to be difficult enough on its own.

Unless it could be compiled into a special binary code that can be linked to by the browser. Then businesses would love it.
Summum ius, summa iniuria.

Anonymously Famous
Posts: 242
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: Coding: Fleeting Thoughts

Postby Anonymously Famous » Mon Sep 12, 2011 7:22 pm UTC

I second the vote for Python. Not only would it potentially make the scripting appear cleaner, but it would mean that everyone would have a Python interpreter. :)

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Mon Sep 12, 2011 7:27 pm UTC

Personally, I would really like to see a statically-typed alternative you could use for browsers. Because of that, I'd actually vote against Python.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Mon Sep 12, 2011 7:46 pm UTC

I'd like to see Scheme as a browser scripting language. It's really a lot like Javascript, but with a cleaner syntax and a more reasonable set of built-in functions. And you can write it all one one line, if you must.

I actually kind of like JavaScript, though. But I hate the whole DOM thing.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests