HTML5 Canvas 2d Context and Javascript Questions

HTML5 Canvas 2d Context and Javascript Questions

Post just about everything that isn't directly related to Spring here!

Moderator: Moderators

Post Reply
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

HTML5 Canvas 2d Context and Javascript Questions

Post by SinbadEV »

So, I've recently been playing around with the future of web technology and have been having trouble interpreting some of the available documentation on certain subjects... so, seeing as this forum seems to be populated with a ready supply of geeks I felt I could ask here (instead of on stack overflow where I have a reputation to maintain)

How do I save the current "drawing" on a canvas to a placeholder object... I'm making a board game and essentially I want to draw the "board", then draw the "pieces" and then draw draw the "available moves" and have them highlighted when the mouse is over them.

so:
Draw Board, Save in State 1
Draw Pieces on Board, Save in State 2
Draw the "locations you can move to" into state 3
Draw Each Path and Save them in an array of states

a list will be presented on the side where the user can select the path they wish to take, when they mouse over the button for the path it will restore state 3 and draw the correct path
after the player choses a move I would restore state 2 and then draw the new locations of the pieses and save to state 2 again
when the player rolls I would draw the locations you can move to and save to state 3... etc.

We used to call this "paging" but I don't know what to call it in HTML5 Canvas 2d Context terms so I'm not even sure what to search for.

there is a "save" and "restore" stack which I guess might save some redrawing but there's got to be some way to store a clip or something into a local object to use later.

EDIT: [Half Solved] Okay, it looks like I can use "image.src = canvas.toDataURL()" to store the canvases state to a PNG formatted image object and then use the "drawImage(image, 0, 0)" to draw it back, but this seems clumsy... it's what I'll use for now though.
User avatar
KaiserJ
Community Representative
Posts: 3113
Joined: 08 Sep 2008, 22:59

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by KaiserJ »

sounds to me like you're going the wrong way round

if you're drawing the board dynamically there should already be adequate arrays and data for you to reconstruct any of these other states without using expensive image copying

use something like z indexing to draw onto the 3 "state" images directly rather than saving/loading the whole thing three times
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by SinbadEV »

OMG, I always forget how much stupid detail is involved with programming... I want to draw to a "non-existent" surface and then display the results... but I can't instantiate a canvas context without it being associated with an existing canvas element in the HTML page.. and the canvas element doesn't support the "collapse" state for visibility so it has to take up physical space... grr... I've resorted to using the following method:

save the context
clear the context
draw the stuff I want
export the drawing to the src property of an Image object
restore the context

due to the fact that it doesn't have a chance to show the user the drawing before it's restored this actually works... though I'd really just rather instantiate a canvas or context that doesn't actually render or take up space... I must just not be programming the way the W3 want me to.
User avatar
Pxtl
Posts: 6112
Joined: 23 Oct 2004, 01:43

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by Pxtl »

Haven't played much with canvases myself, but I'm pretty sure that browser handle double-buffering for you and don't generally display the stuff you drew to the canvas until execution is complete, so you shouldn't need to double-buffer to avoid flicker.

I could be wrong, though.
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by SinbadEV »

Pxtl wrote:Haven't played much with canvases myself, but I'm pretty sure that browser handle double-buffering for you and don't generally display the stuff you drew to the canvas until execution is complete, so you shouldn't need to double-buffer to avoid flicker.

I could be wrong, though.
I'm only buffering because I'm using the active canvas element to draw something that I then want to redraw later

draw board
save board
draw stuff on board
draw board from saved board
draw different stuff on board
draw board from saved board
draw different stuff on board
draw board from saved board

now that I look at it, I don't need to do the save, erase, draw, restore thing for this particular application...

and if what you guys are telling me is correct I would be better off just running the "draw board" function from scratch every time instead running it once and then storing the result... I guess I'm used to real-time situations where drawing everything 60 times a second would slow things down.
User avatar
KaiserJ
Community Representative
Posts: 3113
Joined: 08 Sep 2008, 22:59

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by KaiserJ »

draw the board only once. use z-indexing to display a second, active layer with the stuff you want

there shouldn't be any need for image buffering at all with a turn-based boardgame, as the graphics only update when a piece is selected or a move is made
Andrej
Posts: 176
Joined: 13 Aug 2006, 18:55

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by Andrej »

SinbadEV wrote:We used to call this "paging" but I don't know what to call it in HTML5 Canvas 2d Context terms so I'm not even sure what to search for.
I think it's called offscreen rendering nowdays?
First google result: http://kaioa.com/node/103

Typical of html solved in 3 seconds then it takes 45 minute of screwing with Firebug plugin to make an example if javascript is the future the future sucks >.>
http://ideone.com/m13UU

Ok admittedly the only reason i posted is because this part
SinbadEV wrote:Okay, it looks like I can use "image.src = canvas.toDataURL()" to store the canvases state to a PNG formatted image object and then use the "drawImage(image, 0, 0)" to draw it back
reminds me of Leah's Epsilon-0.30 Clamping Star Algorithm (convert float->string->integer->string->float)

Page was taken down but luckily Anonymous post no. 4 has us covered http://dis.4chan.org/read/prog/1259501835/1-40
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by SinbadEV »

Andrej wrote:
SinbadEV wrote:We used to call this "paging" but I don't know what to call it in HTML5 Canvas 2d Context terms so I'm not even sure what to search for.
I think it's called offscreen rendering nowdays?
First google result: http://kaioa.com/node/103
That's exactly what I was looking for.

Also, thinking I should have included quotes around "the future"...
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by SinbadEV »

On an Unrelated topic I have succeeded in installing a VirtualBox Ubuntu LAMP on my computer that I can connect to with browser, putty and sftp... my development environment is therefore functional... YAY!
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by Forboding Angel »

ungh ./facepalm

WAMP
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by SinbadEV »

Forboding Angel wrote:ungh ./facepalm

WAMP
Isolating my build environment on a virtual server allows me to use this computer for other things without risking blowing it up or exposing it to evil.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: HTML5 Canvas 2d Context and Javascript Questions

Post by AF »

Such a risk, just imagine if we installed these things enmasse on humonguous arrays of machines, stacked in shelves, covering hundreds of thousands of sq meters in a single installation alone..

oh wait..

btw check out processing.js
Post Reply

Return to “Off Topic Discussion”