使用此代码作为基础http://www.script-tutorials.com/demos/158/index.html
我编写以下代码:
$canvas = $('<canvas/>');
ctx = $canvas[0].getContext('2d');
var image = new Image(); // create an image object in memory
image.onload = function () {
// render the image on the canvas
$canvas.width(this.width).height(this.height); // resize the canvas
ctx.drawImage(image, 0, 0, this.width, this.height);
var nav = $("#navigation"); // get the navigation container
// find a pixel about in the middle where the navigation would be
var pos = {
left: nav.position().left+(nav.width()/2),
top: nav.position().top+(nav.height()/2)
}
var pixel = ctx.getImageData(pos.left,pos.top, 1, 1).data;
canvas=null; // no longer need this canvas
// invert the pixel colour, ignoring the alpha channel
var invertedPixelColor = "rgba("+(255-pixel[0])+", "+
(255-pixel[1])+", "+
(255-pixel[2])+", "+
1+")";
nav.css("color",invertedPixelColor); // set the nav text to inverted color
}
image.src = imageURL; // load the image, triggering the calc
我对这个例子有以下问题:
- 为什么图像不能正确渲染到画布上?(在演示中向下滚动以查看渲染的画布)
- 为什么我只能从图像数据中得到 0?
我有
window.console &&
console.log("before: rgba("+pixel[0]+", "+pixel[1]+", "+pixel[2]+", "+pixel[3]+")");
window.console &&
console.log("after: rgba("+(255-pixel[0])+", "+(255-pixel[1])+", "+(255-pixel[2])+", "+1+")");
它返回
before: rgba(0, 0, 0, 0)
after: rgba(255, 255, 255, 1)
我希望我错过了一些简单的东西
更新我是 - 似乎这$("<canvas/>)[0].getContext('2d');
不是 100% 快乐的背景......