从HTML Canvas获取像素? [英] Get a pixel from HTML Canvas?

查看:116
本文介绍了从HTML Canvas获取像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以查询HTML Canvas对象以获取特定位置的颜色?

Is it possible to query a HTML Canvas object to get the color at a specific location?

推荐答案

其中有一个有关W3C文档中的像素操作

There's a section about pixel manipulation in the W3C documentation.

以下是一个示例关于如何反转图像

var context = document.getElementById('myCanvas').getContext('2d');

// Get the CanvasPixelArray from the given coordinates and dimensions.
var imgd = context.getImageData(x, y, width, height);
var pix = imgd.data;

// Loop over each pixel and invert the color.
for (var i = 0, n = pix.length; i < n; i += 4) {
    pix[i  ] = 255 - pix[i  ]; // red
    pix[i+1] = 255 - pix[i+1]; // green
    pix[i+2] = 255 - pix[i+2]; // blue
    // i+3 is alpha (the fourth element)
}

// Draw the ImageData at the given (x,y) coordinates.
context.putImageData(imgd, x, y);

这篇关于从HTML Canvas获取像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆