tags:

views:

42

answers:

1

Hi,

How to reposition/move an object (with its content) within a HTML5 canvas? The flow might be: a) select an area (using triangle, like that of MS paint function icon?) b) move the object to a new location (repositioning) with the content intact.

Would appreciate your help.

A: 

Do you mean something like:

imagedata = ctx.getImageData(sx, sy, sw, sh);
ctx.putImageData(imagedata, dx, dy);

A canvas is already painted/rendered. So it's just pixels, a triangle stops being a triangle after it's drawn. If you want to be able to move objects like triangles you can use svg.

DiggyF
ok, this is very helpful to get me started on this task. Let's forget about triangle now then. Further question, how do we set the starting coordinates (sx,sy) and then specify their width and height (sw,sh). It's got to relate to mousedown, mouseup, mousemove and mousestop (for dx, dy, destination coordinates)? And not even sure if there's such an event trigger called mousestop... care to throw in some demo code?I greatly appreciate it.don
Don Don
http://blog.fpmurphy.com/2010/02/html5-canvas-element-rubber-banding.html gives an example of this.
DiggyF
Thanks, SiggyF, the demo (try) link are not working. The source code looks useful to me, will dig it soon and report back on finding. Don
Don Don
Sorry, can't do the testing for I run into a hiccup. That is, the process prior to is not straightened out yet... or run into ground unexpectedly (see a new post for that coming up in a sec) thanks.
Don Don
SiggyF,I'm populating the canvas with fillText instead. Your sample is too powerful. I attempted with keeping the top portion of your code and removing the DEMO prefix and stopped at doInvertImage function. However, it is not working for me, I honestly do not have a good understanding of what all these iData and its associates are all about... any tutorial or the like? Many thanks.
Don Don