+ 5

Import image sprites in js: fastest

I attach a code where I import .png images for use in an animation. It works but I attempted other ways I could not make work. Question: I am scaling the images every time in draw(), how / where can I scale them just once? The other functions alien1, or defining properties to the image or awaiting a promise to load should work as well. Do they require declarations in the html? (Please, don't give me links but direct answers on the problem 🤗) https://code.sololearn.com/WKf31bV4uIOi/?ref=app

10th Oct 2021, 7:38 AM
bell
bell - avatar
21 Respuestas
+ 8
On the re-scaling: Maybe you could re-size the images once after load, write to a temporary canvas, re-scale and then return the scaled image from there?
10th Oct 2021, 8:21 AM
Lisa
Lisa - avatar
+ 4
bell would be nice to! I'll tell you once I do
11th Oct 2021, 6:12 PM
Vachila64☕
Vachila64☕ - avatar
+ 4
Kode Krasher I will my friend!! xD
14th Oct 2021, 5:47 AM
Vachila64☕
Vachila64☕ - avatar
+ 3
Lisa , this is what I am trying in the two alternatives that do not work. function alien1 writes to canvas and I could choose scale there. defining a variable with image properties would also solve my problem but it displays nothing. what I need is input on what to change in my code
10th Oct 2021, 9:02 AM
bell
bell - avatar
+ 3
bell ah, awesome! I just spent the last 1hr coding on my phone wo make an explanation, lol Oh well😅 https://code.sololearn.com/W3ngK6P5FRUM/?ref=app I wonder how I used my phone to code for the longest time ..it's too stressful, lol
11th Oct 2021, 12:56 PM
Vachila64☕
Vachila64☕ - avatar
+ 3
Vachila64☕ maybe you could make your code public so we can discuss in comments? I would find interesting a benchmark displaying the imagea large number of times. the prerendering overhead happens only once, and it would be good to know if it is worth for the time saving later?
11th Oct 2021, 4:52 PM
bell
bell - avatar
+ 3
Kode Krasher On the offscreen canvas as I understand it: original image (url...) -> offscreen canvas -> resize, create an new image from offscreen canvas ("screenshot") -> drop/ overwrite original image with downsized image I think it would be forgivable if this canvas procedure created some buzz, because it would be only once and before the actual animation. And this is bell 's goal, I think – minimize all resource intensive processes at actual animation run time? edit: with "url" I mean the image as you retrieve it from somewhere else – as Kode Krasher clarified it is of course not necessary to fetch the image anew each time one uses it.
13th Oct 2021, 2:57 PM
Lisa
Lisa - avatar
+ 3
Kode Krasher Ah, tell me before you do! I should probably add like about 2 more checks to it but have grown to lazy to xD
14th Oct 2021, 2:47 AM
Vachila64☕
Vachila64☕ - avatar
+ 2
Revma I do that in drawImage and then it works but I meant, defining it once so as to avoid resizing every time I do not know how in this code: (lines 383,384) https://code.sololearn.com/WfL4CZPfHsWv/?ref=app
10th Oct 2021, 1:40 PM
bell
bell - avatar
+ 2
Martin Taylor Revma I was trying to do precisely this and then using drawImage on r3 (below) but it does not display the aliens. I do not know what I am missing in my code (now it loads the image in a different way). I am not particularly interested in developing games. Actually I am trying to generate Escher tesselations with a few self-organisation rules. So I would like to display motifs out of Escher drawings. I prefer vanilla js to libraries, whatever their merits. I want to make my code more efficient and understand the issues I described. const r3 = { img: new Image(), src: "https://art.pixilart.com/2b2f3aabe30daa5.png", column: 0, row: 0, width: 200, height: 200, scaleWidth: 64, scaleHeight: 64 };
10th Oct 2021, 6:08 PM
bell
bell - avatar
+ 2
Revma yes, what you describe is what my alien1 function tried to do. I have found references about it working, and it works for canvas drawings (see tile function). I use this in my crystallization code but not yet with an external image. Thank you!
10th Oct 2021, 6:11 PM
bell
bell - avatar
+ 2
Martin Taylor i am using drawImage now and scaling the alien many times each frame, it would surely be better to scale it once and copy it every time? As in the r3 definition in my code and previous post. But something must be wrong with it because it does not display when I use it. Thank you
10th Oct 2021, 6:31 PM
bell
bell - avatar
+ 2
Revma this does not answer my question. I have solved the problem in practice in the code. I still have three questions, for which I would like to learn the answer but thanks for trying on the code. Maybe I should trim it down before asking. It is full of stuff. To answer your question, I would anyway prefer to retain the flexibility to scale to be able to adapt to large or small displays. what looks good on an ipad does not necessarily on a phone.
10th Oct 2021, 7:20 PM
bell
bell - avatar
+ 2
Vachila64☕ I will read your code, the time benchmarking is especially interesting. Thanks for taking the time. If you look into my chaos of a code you will see that it contains three prerendering functions. Two were working, the third was not. So it does not save time to scale beforehand? That is unexpected.
11th Oct 2021, 1:29 PM
bell
bell - avatar
+ 2
questions acquire a life of their own... actually, my question was 1- finding an error preventing two of the functions in my code to run 2- which of them would be better (as my working implementation could not be best) it was a loading order issue, resolved calling the function in init. Answers were about many related -and interesting- aspects but not the problem with my code I was asking about. I quite sympathize with anyone my untidy code scares off. Still, thank you to everyone!, it is nice to discuss a topic one is occupied with
13th Oct 2021, 8:54 PM
bell
bell - avatar
+ 2
one of the solutions was actually taking part of a larger image, to cut tiles off. I had seen this in one of Mirielle[ Left SL ] and did not understanfpd why the tile was invisible in mine
13th Oct 2021, 9:30 PM
bell
bell - avatar
+ 1
I could not get the ways called in line 21 or line 24 work
10th Oct 2021, 7:47 AM
bell
bell - avatar
+ 1
Lisa please, dont apologize, thanks for trying to help. i did not explain well 🤗🎂I am frustrated because for instance the .img solution I took from one of mirielle's games and I do not know why it does not work for me. it may be something with loading order
10th Oct 2021, 9:16 AM
bell
bell - avatar
+ 1
Hmm, so the canvas alternative doesn't work for you?
11th Oct 2021, 10:27 AM
Vachila64☕
Vachila64☕ - avatar
+ 1
Vachila64☕ I solved my problem, which I think was indeed related to loading order. Declaring this line in my init function r3.img.src = r3.src; makes it work. Then I can regulate the remaining properties of the r3 constant (at least if I know my source, which I do not since it was one of Caleb's aliens). I think I should delete my question now.
11th Oct 2021, 10:50 AM
bell
bell - avatar