Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Pollyanna
Mar 5, 2005

Milk's on them.


Jabor posted:

Not be default, though many JS frameworks have a convenience function that gives you a random integer in a given range, just because it's so helpful.

If you're not using a framework, writing a small utility function so you can just write randomInt(len(list)) instead of writing the whole Math.floor(... thing every time is probably a good idea.

Hmm, I'm using Coffeescript right now cause it's real similar to Python syntax-wise. Does that count as a framework?

I'm also trying to figure out a decent way to do string formatting in Coffeescript, in a manner similar to printf(). I want o use sprintf() for this purpose and tried to include it into the final .js file, but CS doesn't want to compile whenever I try to use it. My initial attempt was coffee -j sprintf.js stdh.coffee stdh.js. Is that not how it should work?

Adbot
ADBOT LOVES YOU

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
You don't need a library for that. Coffeescript has string interpolation: http://coffeescript.org/#strings

By the way Coffeescript takes more of it's cues from Ruby than Python, and remember that Javascript is VERY different in terms of behaviour to Python and Ruby. (saying this cause I know you're a beginner, don't let the Coffeescript stop you from learning Javascript itself)

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Maluco Marinero posted:

You don't need a library for that. Coffeescript has string interpolation: http://coffeescript.org/#strings

By the way Coffeescript takes more of it's cues from Ruby than Python, and remember that Javascript is VERY different in terms of behaviour to Python and Ruby. (saying this cause I know you're a beginner, don't let the Coffeescript stop you from learning Javascript itself)

Yeah, I was going to say this. In fact, I think I would say you shouldn't use Coffeescript at all until you have a firm grounding in JS.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Jabor posted:

Not be default, though many JS frameworks have a convenience function that gives you a random integer in a given range, just because it's so helpful.

If you're not using a framework, writing a small utility function so you can just write randomInt(len(list)) instead of writing the whole Math.floor(... thing every time is probably a good idea.

Or add the method to Array itself by adding to the prototype, so you can just do myArray.randomEl();

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



MDN's Math.random() page has a couple of implementations for "give me a random number between X and Y" - one for floats and one for ints.

ZeldaLeft
Oct 15, 2004
Waiting in the mirrors of the Hotel Lobby
Crossposting from the wordpress thread:

I have a wordpress theme that doesn't recognize that I have the latest version of Jquery. When the code checks for the version it needs - v1.2.3 or higher, it reads my v1.10.2 as "1.1" and gives me an error message. So, even though I have upgraded to the newest version, a bug prevents my theme from recognizing it.

This seems to be a well-documented issue and is addressed here:
http://wordpress.org/support/topic/...roblem-solution

However, I do not have a file name in my theme folder that has the phrase "blockUI" in it, so I don't know where to copy the code contained in http://malsup.github.io/jquery.blockUI.js and apply it to my site. I do have these files:

In wp-content/themes/zagetti/js there are:
jquery.colorbox-min
jquery.easing.1.3
jquery.effects.core
jquery.mousewheel.min
jquery.ui.totop
jquery-1.5.2.min

And in wp-content/themes/zagetti/js/ui there is:
jquery.effects.core

And in themes/zagetti/js/cp/js there is:
jquery

There are a few others here and there, but that's it for everything in the theme content folder. I don't really know JS, and I cant figure it out. Where do I put this file/code?

ZeldaLeft fucked around with this message at 16:31 on Dec 9, 2013

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

ZeldaLeft posted:

Crossposting from the wordpress thread:

I have a wordpress theme that doesn't recognize that I have the latest version of Jquery. When the code checks for the version it needs - v1.2.3 or higher, it reads my v1.10.2 as "1.1" and gives me an error message. So, even though I have upgraded to the newest version, a bug prevents my theme from recognizing it.

This seems to be a well-documented issue and is addressed here:
http://wordpress.org/support/topic/...roblem-solution

However, I do not have a file name in my theme folder that has the phrase "blockUI" in it, so I don't know where to copy the code contained in http://malsup.github.io/jquery.blockUI.js and apply it to my site. I do have these files:

In wp-content/themes/zagetti/js there are:
jquery.colorbox-min
jquery.easing.1.3
jquery.effects.core
jquery.mousewheel.min
jquery.ui.totop
jquery-1.5.2.min

And in wp-content/themes/zagetti/js/ui there is:
jquery.effects.core

And in themes/zagetti/js/cp/js there is:
jquery

There are a few others here and there, but that's it for everything in the theme content folder. I don't really know JS, and I cant figure it out. Where do I put this file/code?

Easy but terrible solution: roll back to jQuery 1.9 :v:

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

Can you not edit the source of wp plugins? Alternate solution: rename your jquery file name :smugdog:

ZeldaLeft
Oct 15, 2004
Waiting in the mirrors of the Hotel Lobby

A MIRACLE posted:

Can you not edit the source of wp plugins? Alternate solution: rename your jquery file name :smugdog:

Yes, you can, but jquery seems to be integrated into my theme (specifically to to an image slider) and I can't find which files to edit. Which file would I rename? And to what end?

DreadCthulhu
Sep 17, 2008

What the fuck is up, Denny's?!
Had a pretty good experience setting up and using buster.js today. Any thoughts from you guys? Seems to be that it was very chock full of all sorts of useful features and I didn't have to run around the web picking and choosing critical pieces to make it work.

kedo
Nov 27, 2007

Best practice question:

JavaScript code:
var foo = {
    bar: function() {
        if (1>0) {
            return true;
        }
        else {
            return false;
        }
    },
    baz: function() {
        if (foo.bar()) {
            return true;
        }
        else {
             return false;   
        }
    }
}
Is it kosher to set a value in an object literal based on a previously declared value in that same object? It works, but I'm just curious if this is considered bad practice since it's referencing itself.

5TonsOfFlax
Aug 31, 2001
Personally, I would use this.bar() instead of foo.bar() there, but that would change the semantics if you call baz with another object as this. Of course, as is, the behavior will change if the value of foo changes at any time.

ZeldaLeft
Oct 15, 2004
Waiting in the mirrors of the Hotel Lobby
I found this line of code in a blockui.js file in my theme:

*/

;(function($) {

if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);

I assume this is the bit of code that checks to see if im running something above v1.2.3 but doesn't see v1.10.2 as a valid option.
how can I change this so it recognizes (or at least skips by) things properly?

a lovely poster
Aug 5, 2011

by Pipski
add // before the alert

alternatively, change the conditional from

if (x || y)

to

if (false && (x || y))

which will make it never fire

ZeldaLeft
Oct 15, 2004
Waiting in the mirrors of the Hotel Lobby
changing it to this didn't change my error message:
*/;(function($) {if (false && (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.0/.test($.fn.jquery))) {alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);

and neither did this:
*/;(function($) {if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.0/.test($.fn.jquery)) //{alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);

Did I do them both wrong?

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

ZeldaLeft posted:

changing it to this didn't change my error message:
*/;(function($) {if (false && (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.0/.test($.fn.jquery))) {alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);

and neither did this:
*/;(function($) {if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.0/.test($.fn.jquery)) //{alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);

Did I do them both wrong?

Is your browser caching the js maybe?

a lovely poster
Aug 5, 2011

by Pipski

ZeldaLeft posted:

changing it to this didn't change my error message:
*/;(function($) {if (false && (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.0/.test($.fn.jquery))) {alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);

and neither did this:
*/;(function($) {if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.0/.test($.fn.jquery)) //{alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);

Did I do them both wrong?

The first one should work properly, try erasing your cache and/or ensure that the message is coming from that exact line and not another one.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

kedo posted:

Best practice question:

JavaScript code:
var foo = {
    bar: function() {
        if (1>0) {
            return true;
        }
        else {
            return false;
        }
    },
    baz: function() {
        if (foo.bar()) {
            return true;
        }
        else {
             return false;   
        }
    }
}
Is it kosher to set a value in an object literal based on a previously declared value in that same object? It works, but I'm just curious if this is considered bad practice since it's referencing itself.

Based on how you've written your question I'm not sure you know what is going on here. You aren't setting the values to anything other than functions, which happen to reference other functions in the object. The value of baz() will be determined when it's called, not when you declare foo. Of course it's fine for an object to reference parts of itself in it's functions, although as pointed out you might want to use "this" instead of "foo" in the function body.

Are you trying to ask about something like this:
JavaScript code:
var foo = {
    bar: true,
    baz: foo.bar ? true : false
}
or similarly:
JavaScript code:
var foo = {
    bar: (function() {
   	if (1>0) {
            return true;
        }
        else {
            return false;
        }
    })(),
    baz: (function() {
        if (foo.bar) {
            return true;
        }
        else {
             return false;   
        }
    })()
}
where the values are set when foo is declared? I'm not sure if that works or not off the top of my head.

ZeldaLeft
Oct 15, 2004
Waiting in the mirrors of the Hotel Lobby
it was the cache. Thank you so much!!

kedo
Nov 27, 2007

HappyHippo posted:

Based on how you've written your question I'm not sure you know what is going on here.

You're more right than you know - I'm more or less self taught and do not come from a programming background, so I know enough to make things work, but I don't always completely understand the underlying reasons why they work or whether or not I'm doing something the right way. Thus my attempt to learn. :)

HappyHippo posted:

Of course it's fine for an object to reference parts of itself in it's functions, although as pointed out you might want to use "this" instead of "foo" in the function body.

This is really what I was trying to get at. To elaborate a bit to make this a bit less abstract: in the real world my object is just gathering information about a DOM element a user clicked. bar is checking whether the item is main or sub-navigation, and then baz is performing additional logic based on the results. All the information is being gathered at once and I'm not modifying anything in foo after the fact, so I suppose in this case it doesn't really matter if I use "bar" or "this" since they're both going to return the same thing?

I've only just working objects into my code, so I'm still wrapping my brain around 'em to a certain extent.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
Just to be clear, are you trying to collect this information and then store the results in the object foo, or are you trying to attach functions to the object to do the collecting?

kedo
Nov 27, 2007

HappyHippo posted:

Just to be clear, are you trying to collect this information and then store the results in the object foo, or are you trying to attach functions to the object to do the collecting?

The former. Here's a simplified chunk of the actual code, it's part of a URL hashing function. clicked gathering information about what a user clicked, and later I use the values it returns to construct the actual hash.

JavaScript code:
var setMenuHash = function(el) { // Sets the URL hash based on what the user has clicked.

	var clicked = { // Gather information about what the user clicked
		parentType: function() {
			if (el.closest('#menu-nav').length > 0) { // User clicked an item in the main nav
				return 'menu-nav';
			}
			else if (el[0].nodeName === 'SECTION') { // User clicked a section
				return 'section';
			}
			else {
				return false;
			}
		},
		hashValue: function() {
			if (clicked.parentType() === 'menu-nav') {
				var a = el.closest('li').attr('class').split(' ');
				return a[0];
			}
			else if (clicked.parentType() === 'section') {
				return el.attr('id');
			}
			else {
				return false;
			}
		}
	}

	window.location.hash = clicked.hashValue(); // Set the hash
}
One of my goals doing it this way was to keep all of the stuff that's tied to the DOM in one spot. It works as is (though I'm sure there's probably a better way to do it), but I'm unsure whether or not this would be considered the "right" way to use an object in this scenario.

kedo fucked around with this message at 15:41 on Dec 11, 2013

Reality
Sep 26, 2010

kedo posted:

The former. Here's a simplified chunk of the actual code, it's part of a URL hashing function. clicked gathering information about what a user clicked, and later I use the values it returns to construct the actual hash.

One of my goals doing it this way was to keep all of the stuff that's tied to the DOM in one spot. It works as is (though I'm sure there's probably a better way to do it), but I'm unsure whether or not this would be considered the "right" way to use an object in this scenario.

I would not do it like that if I wasn't using clicked.parentType() anywhere else in the setMenuHash object. You are currently doing the same check twice. I wouldn't have the clicked object at all in your current code sample. I'd just set window.location.hash with if statements. I generally use JavaScript objects like your clicked object for encapsulation and code re-use, which doesn't appear in your code sample.

There are like a million ways to do what you are doing and it all depends on your style. The most important thing is that it works and that you (and your coworkers?) can read and maintain it.

kedo
Nov 27, 2007

Reality posted:

I generally use JavaScript objects like your clicked object for encapsulation and code re-use, which doesn't appear in your code sample.

There are like a million ways to do what you are doing and it all depends on your style. The most important thing is that it works and that you (and your coworkers?) can read and maintain it.

Thanks, this is valuable. I'm reusing clicked.parentType() and the entire object itself a couple of times, but probably not enough to justify it. Your second point is really the main reason why I did this all with an object in the first place – I'm handing off a lot of code these days and I want to make sure I'm doing things in a way that make sense to a third party even if it's not the most perfect and beautiful code in the world. Seems that in this case I might have been over thinking it.

Thanks again!

bobua
Mar 23, 2003
I'd trade it all for just a little more.

I've got a node.js app that I want to print from... specifically I want to print an svg image that I have generated to a label printer. I don't know what label printer yet, and I don't know exactly what hardware, but I'm trying to think through the best way to do this.

It's kiosky, so the printer could be connected to the backend server or the actual device the app is displayed on. I'm thinking it will be something like a microsoft surface tablet. What I don't want is a 'print options' dialog popping up. I just want a label to spit out when someone presses a button on the page. I'll have complete control over the tablet, so the app can be in the trusted sites list, a particular browser can be forced, that sort of thing.


Can this be done with some simple javascript or will I need to look at writing a browser addin or separate app to talk to?

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



I have a comment in the last line of one of my scripts that helps Chrome not be useless for debugging it when it's loaded dynamically (downloaded and exec'd, basically):
JavaScript code:
//@ sourceURL=anything.whatever
Some versions of IE (at least IE 9) are interpreting this line, as in running it as part of the script. Does anyone know what the everloving gently caress is up with this?

Edit: I think it only happens when the script is included on a page with a doctype that triggers compatibility mode. Maybe. Need to look into it more.

Munkeymon fucked around with this message at 21:09 on Dec 11, 2013

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

kedo posted:

The former. Here's a simplified chunk of the actual code, it's part of a URL hashing function. clicked gathering information about what a user clicked, and later I use the values it returns to construct the actual hash.

JavaScript code:
var setMenuHash = function(el) { // Sets the URL hash based on what the user has clicked.

	var clicked = { // Gather information about what the user clicked
		parentType: function() {
			if (el.closest('#menu-nav').length > 0) { // User clicked an item in the main nav
				return 'menu-nav';
			}
			else if (el[0].nodeName === 'SECTION') { // User clicked a section
				return 'section';
			}
			else {
				return false;
			}
		},
		hashValue: function() {
			if (clicked.parentType() === 'menu-nav') {
				var a = el.closest('li').attr('class').split(' ');
				return a[0];
			}
			else if (clicked.parentType() === 'section') {
				return el.attr('id');
			}
			else {
				return false;
			}
		}
	}

	window.location.hash = clicked.hashValue(); // Set the hash
}
One of my goals doing it this way was to keep all of the stuff that's tied to the DOM in one spot. It works as is (though I'm sure there's probably a better way to do it), but I'm unsure whether or not this would be considered the "right" way to use an object in this scenario.

Ok, so I apologize if you are already aware of this, but these "values" (parentType and hashValue) aren't values, they're functions you're attaching to the object "clicked". Neither is evaluated until the line "window.location.hash = clicked.hashValue();". I don't see the point of attaching functions to a private object to be used in the setMenuHash function; just declare them as private functions and call them as needed. If it was your intention to evaluate those values you need to understand the difference between these two lines:
code:
function () { /* some stuff */};
(function () { /* some stuff */})();
The first one is just a declaration of a function. The second both declares it and immediately calls it. So
code:
var clicked = { 
	hashValue: function() { ... }
}
just adds a function to the object, while
code:
var clicked = { 
	hashValue: (function() { ... })()
}
takes the return value of the function and saves it in "hashValue". Again, sorry if you were aware of all of this, it just seems from how you've worded the question and labeled the variables that you might be confused about the distinction. If you do want to keep things the way they are now I might suggest changing the names to "getParentType" and "getHashValue" to emphasis that these are functions and not values.

Gism0
Mar 20, 2003

huuuh?

bobua posted:

I've got a node.js app that I want to print from... specifically I want to print an svg image that I have generated to a label printer. I don't know what label printer yet, and I don't know exactly what hardware, but I'm trying to think through the best way to do this.

It's kiosky, so the printer could be connected to the backend server or the actual device the app is displayed on. I'm thinking it will be something like a microsoft surface tablet. What I don't want is a 'print options' dialog popping up. I just want a label to spit out when someone presses a button on the page. I'll have complete control over the tablet, so the app can be in the trusted sites list, a particular browser can be forced, that sort of thing.


Can this be done with some simple javascript or will I need to look at writing a browser addin or separate app to talk to?

It's not possible using JS (It'd be pretty lame if a website could take over your printer) but apparently you can do it with VBScript of all things. Of course It'd only work in IE and I've no idea if it would work on a surface, but it might be worth a try:

code:
<script language='VBScript'>
Sub Print()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>
Then call window.print();

Another option would be to hook the printer to the server running node and expose an API to allow the clients to print. I've never attempted to print from a node app but it's gotta be possible, do some searching on npm and maybe you'll get lucky! Otherwise I'd say your best bet would be to find a CLI print tool and call it as a child process.

kedo
Nov 27, 2007

HappyHippo posted:

Ok, so I apologize if you are already aware of this, but these "values" (parentType and hashValue) aren't values, they're functions you're attaching to the object "clicked".

Please don't apologize! Again, this is all really valuable. Yes I'm aware that they're functions not values (this is another byproduct of not coming from a programming background... I never use the right terminology for stuff), but I think for my purposes doing it one way or the other doesn't really change the final result.

However I guess if I wanted to improve my existing function I could use hashValue: (function() { ... })() (what's the correct term for this?) everywhere instead of what I'm doing now which would limit the number of times I touch the DOM and prevent nearly every private function from being run multiple times. If I'm gathering information about a DOM element every time the function is run, and that information is always going to remain the same, there's no reason to grab it multiple times.

Which now seems like a big "duh." :) I should probably take a Javascript class. Thanks again, you've been incredibly helpful!

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

kedo posted:

Please don't apologize! Again, this is all really valuable. Yes I'm aware that they're functions not values (this is another byproduct of not coming from a programming background... I never use the right terminology for stuff), but I think for my purposes doing it one way or the other doesn't really change the final result.

However I guess if I wanted to improve my existing function I could use hashValue: (function() { ... })() (what's the correct term for this?) everywhere instead of what I'm doing now which would limit the number of times I touch the DOM and prevent nearly every private function from being run multiple times. If I'm gathering information about a DOM element every time the function is run, and that information is always going to remain the same, there's no reason to grab it multiple times.

Which now seems like a big "duh." :) I should probably take a Javascript class. Thanks again, you've been incredibly helpful!

Not sure if that construction has a name or not, it just follows from javascript's syntax. The stuff in brackets declares an anonymous function and then the () calls it right there. There are good reasons to do that but I don't think they apply here. Resist the urge to get fancy, you can just declare clicked and then add the parentType and hashValue values directly, no functions needed.

jony neuemonic
Nov 13, 2009

HappyHippo posted:

Not sure if that construction has a name or not, it just follows from javascript's syntax. The stuff in brackets declares an anonymous function and then the () calls it right there.

I like immediately-invoked function expression.

Pollyanna
Mar 5, 2005

Milk's on them.


I'm using Processing.js to implement Conway's Game of Life, and I'm trying to draw equally sized rectangles at each corner of the canvas to get a hand of PJ. For some reason, though, the rectangles are clearly different sizes, even though my logic is (afaik) correct. :confused: Can someone check my work?

http://jsfiddle.net/ydLKN/38/

edit: Also I'm realizing that my method of creating a 2D array is hosed. What's the best way to do that?

Pollyanna fucked around with this message at 01:40 on Dec 13, 2013

Tres Burritos
Sep 3, 2009

Pollyanna posted:

I'm using Processing.js to implement Conway's Game of Life, and I'm trying to draw equally sized rectangles at each corner of the canvas to get a hand of PJ. For some reason, though, the rectangles are clearly different sizes, even though my logic is (afaik) correct. :confused: Can someone check my work?

http://jsfiddle.net/ydLKN/27/

edit: Also I'm realizing that my method of creating a 2D array is hosed. What's the best way to do that?

change this
code:
processing.rect((processingWidth - tileSize), (processingHeight - tileSize), tileSize, tileSize);
to this

code:
processing.rect((processingWidth - tileSize - 5), (processingHeight - tileSize - 5), tileSize, tileSize);
and they look about the same to me. So Maybe you're not calculating the height of your canvas properly?

Like, the lower right box is going out of the margins it seems. Couldn't say why though.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Even just a -1 will do it, not sure what the deal is there.

http://jsfiddle.net/HdL8W/

* removed the 400x400 attributes on the canvas since they were confusing
* added a border to the canvas so we can position the rectangle a little easier
* put a space between new & Array instead of newArray to get rid of javascript error

edit: oh probably because the pixels are 0 indexed so a size of 250x250 gives you pixels 0 to 249 to work with so you want it to be (244, 244) to (249, 249) for the bottom right rectangle

fletcher fucked around with this message at 01:48 on Dec 13, 2013

Pollyanna
Mar 5, 2005

Milk's on them.


Tres Burritos posted:

change this
code:
processing.rect((processingWidth - tileSize), (processingHeight - tileSize), tileSize, tileSize);
to this

code:
processing.rect((processingWidth - tileSize - 5), (processingHeight - tileSize - 5), tileSize, tileSize);
and they look about the same to me. So Maybe you're not calculating the height of your canvas properly?

Like, the lower right box is going out of the margins it seems. Couldn't say why though.

That is really weird, cause the canvas is 200x200, and the lower right rectangle is spawned at (195, 195) and is 5x5, so it shouldn't be running off the canvas because it's within the boundaries. This implies that the canvas doesn't want to draw past (195, 195) which makes no sense :confused:

I updated the JSFiddle, by the way.

fletcher posted:

Even just a -1 will do it, not sure what the deal is there.

http://jsfiddle.net/HdL8W/

* removed the 400x400 attributes on the canvas since they were confusing
* added a border to the canvas so we can position the rectangle a little easier
* put a space between new & Array instead of newArray to get rid of javascript error

edit: oh probably because the pixels are 0 indexed so a size of 250x250 gives you pixels 0 to 249 to work with so you want it to be (244, 244) to (249, 249) for the bottom right rectangle

:downs: That makes sense. So it should be processing.rect((processingWidth - tileSize - 1), (processingHeight - tileSize - 1), tileSize, tileSize); then, correct? (thanks for changing the JSFiddle around btw)

edit 2: I added some logic to the script for checking the neighbors of a cell and determining if the cell will live or die in the next round. However, I keep getting "Uncaught TypeError: Cannot read property '-1' of undefined" in the console. Can someone check my logic and see if it's right?

http://jsfiddle.net/ydLKN/47/

Pollyanna fucked around with this message at 02:45 on Dec 13, 2013

PleasantDilemma
Dec 5, 2006

The Last Hope for Peace

Pollyanna posted:

edit 2: I added some logic to the script for checking the neighbors of a cell and determining if the cell will live or die in the next round. However, I keep getting "Uncaught TypeError: Cannot read property '-1' of undefined" in the console. Can someone check my logic and see if it's right?

http://jsfiddle.net/ydLKN/47/

Most cells have neighbors on 8 sides, but if the cell is on the edge of the canvas then it might only have 5 or 3 neighbors. Where you declare cellNeighbors you do a lot of [x -1] and [y +1] but x could be zero and y could be the top of the array.

bobua
Mar 23, 2003
I'd trade it all for just a little more.

Gism0 posted:

It's not possible using JS (It'd be pretty lame if a website could take over your printer) but apparently you can do it with VBScript of all things. Of course It'd only work in IE and I've no idea if it would work on a surface, but it might be worth a try:

Another option would be to hook the printer to the server running node and expose an API to allow the clients to print. I've never attempted to print from a node app but it's gotta be possible, do some searching on npm and maybe you'll get lucky! Otherwise I'd say your best bet would be to find a CLI print tool and call it as a child process.

Yeah, knew about the security issue, was just really hoping having complete control of the browser would mitigate that. Guess not.

Really sucks, I'm shocked with all the web apps that printing like this hasn't been squared away. Thanks though.

biznatchio
Mar 31, 2001


Buglord

bobua posted:

was just really hoping having complete control of the browser would mitigate that. Guess not.

Having complete control of the browser does mitigate the problem, because you can always write and install a browser addin to allow printing to be scripted.

Having complete control of the browser means literally nothing if you're only talking about Javascript running on a webpage. The complete control part comes in all the stuff you can do outside the webpage's own sandbox.

See also Chrome's "--kiosk" and "--kiosk-printing" command line arguments. See here for more info.

biznatchio fucked around with this message at 01:13 on Dec 15, 2013

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

bobua posted:

Yeah, knew about the security issue, was just really hoping having complete control of the browser would mitigate that. Guess not.

Really sucks, I'm shocked with all the web apps that printing like this hasn't been squared away. Thanks though.

The term 'web app' is a marketing thing - web applications are not first class citizens. It's very easy for desktop applications to do stuff like drag and drop between applications, print, save files to the hard drive, have keyboard shortcuts and accelerators and menu bars and floating toolbars, and a whole host of other poo poo that people take for granted in the desktop world that doesn't translate to the web/mobile experience.

The worst thing your marketing team can do is look at an existing desktop application and go
"Take this and make it be a webapp." That's why all that activex/client side java poo poo got super popular back in they day - people would just write lovely activex controls and voila, "webapp."

There has been some progress in providing richer web/mobile platforms, but the progress has been made primarily in understanding the target platforms better, keeping it simple, and not trying to recreate the desktop experience.

Adbot
ADBOT LOVES YOU

bobua
Mar 23, 2003
I'd trade it all for just a little more.

biznatchio posted:


See also Chrome's "--kiosk" and "--kiosk-printing" command line arguments. See here for more info.

I was literally going to order a retarded network label printer for way too much money and set about doing all this on the back end and blah blah blah. Cannot believe this didn't show up when googling for a solution. Thank you.

If you would like a forums upgrade or t-shirt with a sloth on it, pm me.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply