I am trying to follow this example http://www.script-tutorials.com/html5-canvas-image-zoomer/
I get the following error:
Object doesn't support property or method 'getContext'
here is my script:
stage = new Kinetic.Stage({
container: 'container',
width: 512,
height: 512
});
var layer = new Kinetic.Layer();
var imageObj = new Image();
imageObj.onload = function() {
var yoda = new Kinetic.Image({
x: 0,
y: 0,
image: imageObj,
width: 512,
height: 512
});
layer.add(yoda);
stage.add(layer);
};
imageObj.src = "../../Content/images/notfound.png";
canvas = document.getElementById('container');
ctx = canvas.getContext('2d');
Would appreciate your suggestions or is there an example for image magnifier for Kinetic. Thanks in advance
When you did the following, it gets the div element hosting kineticjs...it doesn't get a canvas.
That's why your call to
getContext
fails.[Edited to include an example of magnification using Kinetic's custom Shape]
We can use the Kinetic Shape object which is designed to allow us to do custom drawing.
We can custom draw anything in the
drawFunc
function because we have access to the canvas context.drawfunc
will be called every time the custom Shape needs to be redrawn.Here is the Kinetic custom Shape object in outline form:
Now for some zoomer specifics.
First, use a temporary html canvas to create a copy of the image at ½ resolution:
In the
drawFunc
function of the Shape, draw a rectangle containing the half-resolution image.Notice that
drawFunc
must conclude withcanvas.fillStroke(this)
canvas.fillStroke(this)
is a KineticJS specific command that renders the drawings and it is required.If the mouse is down, also draw the zoom viewer with a cropped portion of the full size image.
That’s it...See the code below for some fine-points and styling.
Here’s a Fiddle that must be viewed in Chrome or Mozilla (IE=CORS exception): http://jsfiddle.net/m1erickson/dMr8g/
Here is example code: