Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does canvas element have "change" event?

Is there a way to attach event handler to a change of a canvas element? I need to fire a function whenever something draws anything on it.

like image 645
Michal Till Avatar asked Jan 10 '11 15:01

Michal Till


People also ask

What does the canvas element do?

<canvas> is an HTML element which can be used to draw graphics via scripting (usually JavaScript). This can, for instance, be used to draw graphs, combine photos, or create simple animations.

How do I change the color of my element in canvas?

In a sense, you can't really "change" the color of an element on the canvas because it has no scene graph, or, in other words, it has no history of what has been drawn on the canvas. To change the color of a line, you would have to redraw the line. ctx. moveTo(0, 0); ctx.

What is the function of the new HTML5 canvas element?

The CANVAS element allows you to add so much more interactivity to your web pages because now you can control the graphics, images, and text dynamically with a scripting language. The CANVAS element helps you turn images, photos, charts, and graphs into animated elements.

Can canvas have child elements?

The Chart Canvas element also uses the following child elements to generate, control, and format the canvas and charts. Select the element link for more information. Specifies properties of axis lines, grid lines, tick marks, and label fonts.


1 Answers

No, the canvas element does not provide any events, it's just a plain bitmap.

If you really want to do this, hijack all the calls on a context built in your events and then call the original function (which you have copied previously).

My advice:
Do not do the above, it will be slow and hard to maintain. Instead change the design of your application, you could, for one, make custom drawing functions which will abstract the canvas and put in the event stuff there.

This would also have the added benefit of less context.* calls (therefore cleaner code) when doing a lot of drawing.

like image 111
Ivo Wetzel Avatar answered Sep 17 '22 12:09

Ivo Wetzel