While trying to create a custom shape using KineticJS (v4.5.4) I noticed that the Kinetic.Shape class drawFunc method is called twice, one more time than what I was expecting given the code below (adapted from http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-shape-tutorial/). When I run the code below I can see that on my browser console "DEBUG" is printed twice, indicating that the method in question is called twice, why is that??
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"></script>
<script defer="defer">
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
/*
* create a triangle shape by defining a
* drawing function which draws a triangle
*/
var triangle = new Kinetic.Shape({
drawFunc: function(canvas) {
console.log('DEBUG');
var context = canvas.getContext();
context.beginPath();
context.moveTo(200, 50);
context.lineTo(420, 80);
context.quadraticCurveTo(300, 100, 260, 170);
context.closePath();
canvas.fillStroke(this);
},
fill: '#00D2FF',
stroke: 'black',
strokeWidth: 4
});
// add the triangle shape to the layer
layer.add(triangle);
// add the layer to the stage
stage.add(layer);
</script>
</body>
</html>
Cheers
KineticJS may invoke the
drawFunc
any time it chooses. You should design your shape'sdrawFunc
to be invoked repeatedly and at any time. The exact question of whydrawFunc
is invoked at any particular time is a concern for the library designers, who work hard to make sure you never need to worry about such questions as when and how oftendrawFunc
ought to be invoked.But since you're asking... I put together a modification of your code that captures the
canvas
argument for each call todrawFunc
.canvas.element.parentNode
is the container<div>
, so this is clearly the on-page canvas.canvas.element.parentNode
isnull
, which means that the canvas is not currently attached to the DOM.KineticJS could have a working off-page canvas for a number of reasons:
drawImage
to quickly draw the whole shape, instead of redrawing each separate line)EDIT
In the specific case of KinecticJS, the hidden canvas seems to be used for event detection (e.g., determining whether a click has fallen in the bounds of a drawn object or not):