data:
format;var canvas = document .getElementById( "cv" ),<br>
ctx = canvas.getContext( "2d" );<br>
<br>
ctx.setFillStyle( "#ff0" )<br>
.setStrokeStyle( "#0ff" )<br>
.strokeRect(10, 20, 30, 30)<br>
.fillRect(30, 40, 50, 50)<br>
.invoke( "getImageData" , 0, 0, canvas.width, canvas.height, function (imageData) { <br>
// ... imageData - getImageData <br>
} );<br>
In this example, the handler is called after the context commands are executed (and yes, this is a chain of commands).var canvas = document .getElementById( "cv" ),<br>
ctx = canvas.getContext( "2d" ),<br>
image_src = "sample.jpg" ;<br>
<br>
canvas.onload = function (img) { <br>
if (img.src.indexOf(image_src) > -1) { <br>
ctx.drawImage(img, 10, 10);<br>
} <br>
} <br>
canvas.loadImage(image_src);<br>
In addition, the pictures can be in the data:
URI format.var canvas = document .getElementById( "cv" ),<br>
ctx = canvas.getContext( "2d" );<br>
<br>
ctx.invoke( "getImageData" , 0, 0, canvas.width, canvas.height, function (buf)<br>
{ <br>
for ( var y = 0; y < canvas.height; y++)<br>
{ <br>
for ( var x = 0; x < canvas.width; x++)<br>
{ <br>
var ofs = y * canvas.width + x,<br>
pixelValue = buf.data [ ofs ] ,<br>
red = pixelValue.charCodeAt(0),<br>
green = pixelValue.charCodeAt(1),<br>
blue = pixelValue.charCodeAt(2),<br>
alpha = pixelValue.charCodeAt(3);<br>
<br>
buf.data [ ofs ] = String .fromCharCode(red % 32, green % 64, blue % 128, alpha);<br>
} <br>
} <br>
ctx.invoke( "putImageData" , buf, 0, 0, function () { ;<br>
// ... <br>
console.log( "Image data dump:" + buf);<br>
// , Internet Explorer \x00. <br>
} );<br>
} );<br>
And yes, it works rather slowly in Internet Explorer, unlike its competitors (although there is some solution to this problem, it will be added in the next version).isPointInPath(x, y)
method returns a positive value if x, y is within the contour boundaries. In other browsers, if inside a contour fill.toDataURL()
:var canvas = document .getElementById( "cv" );<br>
var type = "image/jpg" , quality = .4; // - <br>
canvas.toDataURL(type, quality, function (png_data) { <br>
// <br>
var ctx = this .getContext( "2d" );<br>
this .onload = function (img) { <br>
ctx.drawImage(img, 0, 0, canvas.width - 100, 0, 100, 100);<br>
} <br>
this .loadImage(png_data);<br>
} );<br>
This is similar to invoke, where the return value is passed to the function-handler.source-over
and lighter
implemented in fxCanvas. But in the future the rest will be added.Source: https://habr.com/ru/post/91130/
All Articles