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.
<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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With