gpt4 book ai didi

javascript - 更改 Canvas 上图像的颜色/色调的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-03 08:38:05 24 4
gpt4 key购买 nike

我尝试用谷歌搜索答案,但我从来没有找到一个简单的答案,或者为什么这个答案有效。所以我想知道,用来改变 Canvas 上图像颜色的代码是什么?例如,假设我想将内容更改为 #ff0000。如果像素是#000000,它应该保持这种状态。如果像素使用 #ffffff,则应变为 #ff0000。这是我的文本对象构造函数:

Text = function(x, y, str, s, c){
var img = new Image();
img.src = "font.png";
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ".", "!", "?"]
this.text = str.toLowerCase();
this.x = x;
this.y = y;
this.type = "text";
this.rows = 1;
this.height = this.rows * 10;
this.width = this.text.length * 8;
this.draw = function(){
for (var i = 0; i < this.text.length; i++)
{
scene.ctx.drawImage(img, (alphabet.indexOf(this.text[i]) % 16) * 8, Library.newMath.roundZero(alphabet.indexOf(this.text[i]) / 16) * 10, 8, 10, this.x + i * 8 + (scene.xOffset * !this.fixed), this.y + 0 + (scene.yOffset * !this.fixed), 8, 10);
}
}
Shape.prototype.constructor.call(this, x, y);
}

最佳答案

markE 为您提供了实现您想要的目标的全部 key in comments但这里它适用于您的情况:

您声明您的图像仅由一种纯色(白色)组成。
(这里我会认为透明没有颜色,但在其他情况下它是)
要更改此颜色,您可以使用 ctx.globalCompositeOperation('source-atop')参数,它将“仅在与现有 Canvas 内容重叠的地方”绘制新形状。这意味着您可以先绘制字母形状,然后在其上填充一个矩形,并且只会绘制彩色字母形状。

var ctx = canvas.getContext('2d');
var img = new Image();
img.onload = function() {
canvas.width = this.width;
canvas.height = this.height;
ctx.fillStyle = 'red';
ctx.drawImage(this, 0, 0);
ctx.globalCompositeOperation = 'source-atop';
ctx.fillRect(0, 0, canvas.width, canvas.height)
// reset to default
ctx.globalCompositeOperation = 'source-over';
}
img.src = "/image/9RQXh.png";
<canvas id="canvas"></canvas>

或者您也可以用其他方法来完成:首先绘制彩色矩形,将globalCompositeOperation设置为“destination-atop”,然后绘制您的字母。

var ctx = canvas.getContext('2d');
var img = new Image();
img.onload = function() {
canvas.width = this.width;
canvas.height = this.height;
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.globalCompositeOperation = 'destination-atop';
ctx.drawImage(this, 0, 0);
// reset to default
ctx.globalCompositeOperation = 'source-over';
}
img.src = "/image/9RQXh.png";
<canvas id="canvas"></canvas>

现在,要在代码中实现它,您可以保存 Text 对象中设置的彩色字母并绘制它而不是原始图像:

Text = function(x, y, str, s, c){
var img = new Image();
// save a pointer to our object so we can use it in the onload event
var that = this;
// here implement the coloring part
img.onload = function(){
that.img = document.createElement('canvas');
that.img.width = this.width;
that.img.height = this.height;
var ctx = that.img.getContext('2d');
// set the color to the wanted one
ctx.fillStyle = c;
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.globalCompositeOperation = 'destination-atop';
ctx.drawImage(this, 0, 0);
}
img.src = "font.png";
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ".", "!", "?"]
this.text = str.toLowerCase();
this.x = x;
this.y = y;
this.type = "text";
this.rows = 1;
this.height = this.rows * 10;
this.width = this.text.length * 8;
this.draw = function(){
for (var i = 0; i < this.text.length; i++)
{
// here use the canvas we created
scene.ctx.drawImage(this.img, (alphabet.indexOf(this.text[i]) % 16) * 8, Library.newMath.roundZero(alphabet.indexOf(this.text[i]) / 16) * 10, 8, 10, this.x + i * 8 + (scene.xOffset * !this.fixed), this.y + 0 + (scene.yOffset * !this.fixed), 8, 10);
}
}
Shape.prototype.constructor.call(this, x, y);
}

关于javascript - 更改 Canvas 上图像的颜色/色调的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33135691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com