![]() |
Images not displayed when iterated
Hi all,
I'm not a professional programmer and fairly new to JavaScript, so please don't laugh. Here goes nothing. I'm trying to display 10 images pulled from a database using PHP/MySQL next to eachother (to use as source canvas for a scrolling routine in another canvas). This works (assume all variables and arrays have been defined properly - I'm merely showing the important part): Code:
img[0] = new Image();Code:
for (i=0; i<number; i++) { |
Number is a keyword (or type) in JS so I wouldn't use that as a variable name.
So width is an array but height isn't? Personally, I would not use width or height as identifiers either. check that width[i] is a number, rather than a string or something else, so that it can be ADDED to sourcepointer. |
Thank you for your reply so far!
There's no array for height as the height of all pictures is the same (they are automatically resized to the canvas height, while preserving the aspect ratio). Unfortunately changing the code into this: Code:
for (i=0; i<numberofpics; i++) {widthpic definitely contains a value, as... Code:
img[0] = new Image();ctxsrc.drawImage(img[i], sourcepointer, 0, widthpic[i], heightcanvas); <= Uncaught TypeError: Type error (repeated 9 times) |
I think this is maybe a closure issue. the onload events don't fire until all the loops have finished, in which case i is 9 (or 10) for all of the images.
Code:
for (i=0; i<numberofpics; i++) { |
No luck yet, but:
Code:
for (i=0; i<numberofpics; i++) { |
If it is related to loading times then try reducing the number of images temporarily to see if it behaves better. If this is the case then creating timeouts may not help as it (I'm assuming) needs to wait until the first drawImage completes before starting the second, etc..
Hopefully this is not the issue as it may prove tricky to stagger the image-drawing. Try outputting the variable information each time within the loop: Code:
console.log(sourcepointer); // etc.Disclaimer: I haven't done much work with canvas. |
You may need to remember the values for sourcepointer as well:
Code:
for (i=0; i<numberofpics; i++) { |
No luck I'm afraid...
I also tried: Code:
for (i=0; i<numberofpics; i++) { |
My last attempt:
Code:
for (i=0; i<numberofpics; i++) { |
I read a bit about asynchronous script execution and functions in functions. Then I read about callback hell. I read and read, but the whole concept of functions inside functions was making me dizzy.
Until I found this page: http://callbackhell.com/ I still didn't understand half of it, but one line hit me: "Write small modules that each do one thing, and assemble them into other modules that do a bigger thing. You can't get into callback hell if you don't go there." Then I came up with this: Code:
var loadPictureOnCanvas = function (index, url, sourcepointer, widthpic, heightcanvas) { |
This script works a lot better, as it uses callback (I think) in a way I think I understand. I've tried some more convoluting syntaxes (I believe this is called callback hell), but I can't get my head around them.
This loads and displays all 10 images fine. However, the copy to the new canvas fails because JS is trying to copy the images from the source canvas while they are not displayed yet. However, all of them have neatly been preloaded, as the copying function is not executed unless the source canvas is ready and contains all pictures. When I insert alerts though, the source canvas clearly shows that the pictures display there *after* the (still empty) source canvas has been copied to the target canvas. Is there some time between loading and displaying? Apparently. If so, how do I check on that? Code:
var loadPictureOnCanvas = function (index, url, sourcepointer, widthpic, heightcanvas) { |
Code:
copyCanvas(makeSourceCanvas());..but your function Code:
var copyCanvas = function() { |
I really appreciate all the hints.
However: Code:
var loadPictureOnCanvas = function (index, url, sourcepointer, widthpic, heightcanvas) {Now I have never worked with asynchronous code before, so I may be entirely missing the purpose of callbacks... Apparently I do, else this would have worked. |
As I'm scrolling pixel by pixel anyway I just gave up on the whole thing and decided to just start scrolling whether the source canvas' contents have been copied or not. In a worst-case-scenario, I miss two pixels or so, but this is not detectable by the human eye.
Code:
window.requestAnimFrame = (function(callback) {See the script in action at www.japanology.nl (top left corner). |
Solution found. This guy nails it down...
This guy nails it down... http://www.impressivewebs.com/callba...ns-javascript/
The first proper explanation about callback I see on the web that can actually be understood by stupid beginners like me. I have finally seen the light. After that, solving the problem was pretty easy: Code:
<script> |
| All times are GMT +1. The time now is 08:36 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.