javascript - 事件发生时如何从html画布图像中删除叠加效果
问题描述
我想用输入类型颜色事件监听器改变 T 恤颜色。画布上的图像很好。但是,每当用户更改颜色并触发事件时,该颜色的叠加效果就会出现在图像上。我想删除那种叠加效果。我希望图像看起来与起点处的蓝色完全一样,没有覆盖。
<input id="selectedColor" type="color" value="#0000ff">
<canvas id="myDrawing" width="530" height="600">
var x; //drawing context
var width; // canvas width
var height; // canvas height
var fg; //image
var buffer; //inner canvas
var color = '#0000ff'; //default color
function shirtColor(){
return color;
}
function updateShirtColor(){
//update value of color
var currentColor = document.getElementById('selectedColor').value;
color = currentColor;
}
window.onload = function() {
var myColor = document.getElementById('selectedColor');
myColor.addEventListener('change',draw);
var drawingCanvas = document.getElementById('myDrawing');
// Check the element is in the DOM and the browser supports canvas
if(drawingCanvas && drawingCanvas.getContext) {
// Initaliase a 2-dimensional drawing context
width = drawingCanvas.width;
height = drawingCanvas.height;
x = drawingCanvas.getContext('2d');
// grey box grid for transparency testing
x.fillStyle = shirtColor();
x.fillRect(0,0,width,height);
fg = new Image();
fg.src = 'https://d1b2zzpxewkr9z.cloudfront.net/images/products/apparel/product_type_1_front.png';
// create offscreen buffer,
buffer = document.createElement('canvas');
buffer.width = fg.width;
buffer.height = fg.height;
bx = buffer.getContext('2d');
// fill offscreen buffer with the tint color
bx.fillStyle = '#FF';
bx.fillRect(0,0,buffer.width,buffer.height);
// destination atop makes a result with an alpha channel identical to fg, but with all pixels retaining their original color *as far as I can tell*
bx.globalCompositeOperation = "destination-atop";
bx.drawImage(fg,0,0);
// to tint the image, draw it first
x.drawImage(fg,0,0);
//then set the global alpha to the amound that you want to tint it, and draw the buffer directly on top of it.
x.globalAlpha = 0.5;
x=x.drawImage(buffer,0,0);
}
}
function draw(){
updateShirtColor();
var drawingCanvas = document.getElementById('myDrawing');
// Check the element is in the DOM and the browser supports canvas
if(drawingCanvas && drawingCanvas.getContext) {
// Initaliase a 2-dimensional drawing context
width = drawingCanvas.width;
height = drawingCanvas.height;
x = drawingCanvas.getContext('2d');
x.clearRect(0, 0,width,height);
// grey box grid for transparency testing
x.fillStyle = shirtColor();
x.fillRect(0,0,width,height);
fg = new Image();
fg.src = 'https://d1b2zzpxewkr9z.cloudfront.net/images/products/apparel/product_type_1_front.png';
// create offscreen buffer,
buffer.width = fg.width;
buffer.height = fg.height;
bx = buffer.getContext('2d');
// fill offscreen buffer with the tint color
bx.clearRect(0, 0, buffer.width, buffer.height);
bx.fillStyle = '#FF';
bx.fillRect(0,0,buffer.width,buffer.height);
// destination atop makes a result with an alpha channel identical to fg, but with all pixels retaining their original color *as far as I can tell*
bx.globalCompositeOperation = "destination-atop";
bx.drawImage(fg,0,0);
// to tint the image, draw it first
x.drawImage(fg,0,0);
//then set the global alpha to the amound that you want to tint it, and draw the buffer directly on top of it.
x.globalAlpha = 0.5;
x=x.drawImage(buffer,0,0);
}
}
我希望在发生颜色变化事件后有清晰的图像而没有叠加效果。
解决方案
我不太确定这是您要问的。您的代码中有错误。您尝试在图像加载之前绘制图像。我已经将那部分代码包装在fg.onload = function() {....}
您遇到的第二个错误:在函数中draw
,每次调用该函数时都会创建一个新图像。你不需要。你已经有了fg
变量。就用它。
var x; //drawing context
var width; // canvas width
var height; // canvas height
var fg; //image
var buffer; //inner canvas
var color = "#0000ff"; //default color
function shirtColor() {
return color;
}
function updateShirtColor() {
//update value of color
var currentColor = document.getElementById("selectedColor").value;
color = currentColor;
}
window.onload = function() {
var myColor = document.getElementById("selectedColor");
myColor.addEventListener("change", draw);
var drawingCanvas = document.getElementById("myDrawing");
// Check the element is in the DOM and the browser supports canvas
if (drawingCanvas && drawingCanvas.getContext) {
// Initaliase a 2-dimensional drawing context
width = drawingCanvas.width;
height = drawingCanvas.height;
x = drawingCanvas.getContext("2d");
// grey box grid for transparency testing
x.fillStyle = shirtColor();
x.fillRect(0, 0, width, height);
fg = new Image();
fg.src =
"https://d1b2zzpxewkr9z.cloudfront.net/images/products/apparel/product_type_1_front.png";
fg.onload = function() {
// create offscreen buffer,
buffer = document.createElement("canvas");
buffer.width = fg.width;
buffer.height = fg.height;
bx = buffer.getContext("2d");
// fill offscreen buffer with the tint color
bx.fillStyle = "#FF";
bx.fillRect(0, 0, buffer.width, buffer.height);
// destination atop makes a result with an alpha channel identical to fg, but with all pixels retaining their original color *as far as I can tell*
bx.globalCompositeOperation = "destination-atop";
bx.drawImage(fg, 0, 0);
// to tint the image, draw it first
x.drawImage(fg, 0, 0);
//then set the global alpha to the amound that you want to tint it, and draw the buffer directly on top of it.
//x.globalAlpha = 0.5;
//x=x.drawImage(buffer,0,0);
};
}
};
function draw() {
updateShirtColor();
var drawingCanvas = document.getElementById("myDrawing");
// Check the element is in the DOM and the browser supports canvas
if (drawingCanvas && drawingCanvas.getContext) {
// Initaliase a 2-dimensional drawing context
width = drawingCanvas.width;
height = drawingCanvas.height;
x = drawingCanvas.getContext("2d");
x.clearRect(0, 0, width, height);
// grey box grid for transparency testing
x.fillStyle = shirtColor();
x.fillRect(0, 0, width, height);
// create offscreen buffer,
buffer.width = fg.width;
buffer.height = fg.height;
bx = buffer.getContext("2d");
// fill offscreen buffer with the tint color
bx.clearRect(0, 0, buffer.width, buffer.height);
bx.fillStyle = "#FF";
bx.fillRect(0, 0, buffer.width, buffer.height);
// destination atop makes a result with an alpha channel identical to fg, but with all pixels retaining their original color *as far as I can tell*
bx.globalCompositeOperation = "destination-atop";
bx.drawImage(fg, 0, 0);
// to tint the image, draw it first
x.drawImage(fg, 0, 0);
//then set the global alpha to the amound that you want to tint it, and draw the buffer directly on top of it.
//x.globalAlpha = 0.5;
//x=x.drawImage(buffer,0,0);
}
}
<input id="selectedColor" type="color" value="#0000ff">
<canvas id="myDrawing" width="530" height="600">
推荐阅读
- android - 安卓机房:自动查询代码不校验完整性
- json - 在颤动中使用 Chopper 库将 JSON 响应转换为模型对象?
- php - 如何在 MacOS catalina、Xampp VM 和 php7.3 中安装 php_gmp
- serialization - Rust bincode enum 二进制序列化
- javascript - mysql pool.getConnection 失败会破坏代码
- c# - 如何使用 Visual Studio Code 在 C# 中创建 GUI 应用程序?
- javascript - Javascript无法读取非标签
- javascript - 从浏览器中粘贴事件之外的剪贴板读取文件
- discord.py - 让机器人创建一个 ytdl 来播放音乐
- java - 如何将菜单的方法外包给单独的类?