使用此代碼作爲基礎下面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)
我希望我失去了一些東西簡單
UPDATE我 - 它似乎$("<canvas/>)[0].getContext('2d');
不是100%愉快的背景...
爲什麼這個否決後4年? – mplungjan