简体   繁体   English

将 canvas 背景颜色更改为透明

[英]Change canvas background color to transparent

I have a text under canvas, i want to show it when i am erasing background color of canvas.我在 canvas 下有一个文本,我想在擦除 canvas 的背景颜色时显示它。 Now it is red, when i wrote transparent it does not work.现在它是红色的,当我写透明时它不起作用。 I need to show that text when i draw with the mouse.当我用鼠标绘图时,我需要显示该文本。 I tried with rgba too, but is was not working.我也尝试过使用 rgba,但是没有用。

please help me if you can如果可以,请你帮助我

enter code here

 var cont = document.getElementById("spots"), // UI elements canvas = document.getElementById("canvas"), alpha = document.getElementById("alpha"), modes = document.getElementById("modes"), ctx = canvas.getContext("2d"), isDown = false, // defaults color = "red"; // set up color palette using a custom "Spot" object // This will use a callback function when it is clicked, to // change current color function Spot(color, cont, callback) { var div = document.createElement("div"); div.style.cssText = "width:50px;height:50px;border:1px solid #000;margin:0 1px 1px 0;background:" + color; div.onclick = function() {callback(color)}; cont.appendChild(div); } // add some spot colors to our palette container new Spot(color, cont, setColor); // this will set current fill style based on spot clicked function setColor(c) {ctx.fillStyle = c} // setup defaults ctx.fillStyle = color; ctx.globalAlpha = 1; // events canvas.onmousedown = function() {isDown = true}; window.onmouseup = function() {isDown = false}; window.onmousemove = function(e) { if (;isDown) return. var r = canvas,getBoundingClientRect(). x = e.clientX - r,left. y = e.clientY - r;top. ctx;beginPath(). ctx,arc(x, y, 25, 0. 2*Math;PI). ctx;fill(); };
 .main-content{ position: relative; width: 300px; height: 300px; }.main-text{ position: absolute; right: 0; left: 0; text-align: center; z-index: 8; font-size: 35px; } #canvas{ background-color: green; position: absolute; z-index: 9; }
 <div class="main-content"> <p class="main-text">You Won:;!</p> <canvas id="canvas" width=300 height=300 style="border: 1px solid green;"></canvas> <div id="spots"></div> </div>

I think you would like to get something like the solution in the snippet below.我想你想得到类似下面片段中的解决方案。

 var cont = document.getElementById("spots"), // UI elements canvas = document.getElementById("canvas"), alpha = document.getElementById("alpha"), modes = document.getElementById("modes"), ctx = canvas.getContext("2d"), isDown = false, // defaults color = "green"; // set up color palette using a custom "Spot" object // This will use a callback function when it is clicked, to // change current color function Spot(color, cont, callback) { var div = document.createElement("div"); div.style.cssText = "width:50px;height:50px;border:1px solid #000;margin:0 1px 1px 0;background:" + color; div.onclick = function() { callback(color) }; cont.appendChild(div); } // add some spot colors to our palette container new Spot(color, cont, setColor); // this will set current fill style based on spot clicked function setColor(c) { ctx.fillStyle = c } // setup defaults ctx.fillStyle = color; ctx.globalAlpha = 1; // create a rectangle using canvas functions, not CSS // background color. const createRect = (ctx, width, height) => { ctx.fillRect(0, 0, width, height) } createRect(ctx, 300, 300) // events canvas.onmousedown = function() { isDown = true }; window.onmouseup = function() { isDown = false }; window.onmousemove = function(e) { if (;isDown) return. var r = canvas,getBoundingClientRect(). x = e.clientX - r,left. y = e.clientY - r;top: // you needed a bit more code here. ctx,fillStyle = "rgba(255, 255, 255. 0.5)" ctx;save(). ctx;globalCompositeOperation = 'destination-out'. ctx;beginPath(). ctx,arc(x, y, 25, 0. 2 * Math,PI; false). ctx;fill(). ctx;restore(); };
 .main-content { position: relative; width: 300px; height: 300px; }.main-text { position: absolute; right: 0; left: 0; text-align: center; z-index: 8; font-size: 35px; } #canvas { /*background-color: green;*/ position: absolute; z-index: 9; }
 <div class="main-content"> <p class="main-text">You Won:;!</p> <canvas id="canvas" width=300 height=300 style="border: 1px solid green;"></canvas> <div id="spots"></div> </div>

About canvas global compositions: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation关于 canvas 全局组成: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation

You are halfway there, but there are a couple of things to change.你已经完成了一半,但有几件事需要改变。 What you're trying to do is to change the appearance of a css property through the canvas, which does not work like that.您正在尝试做的是通过 canvas 更改 css 属性的外观,这不起作用。 You also can not alter the transparency of the canvas, however there are solutions to your case, and it is very simple.您也无法更改 canvas 的透明度,但是有针对您的情况的解决方案,并且非常简单。

You need to apply a background color to your canvas, then remove the pixels by using the exact same drawing function you have, then you set an extra property called globalCompositeOperation , which is basically the "BlendMode" of photoshop.您需要为 canvas 应用背景颜色,然后使用完全相同的绘图 function 删除像素,然后设置一个名为globalCompositeOperation的额外属性,这基本上是 photoshop 的“混合模式”。

So here's your updated code:所以这是你更新的代码:

 var cont = document.getElementById("spots"), // UI elements canvas = document.getElementById("canvas"), alpha = document.getElementById("alpha"), modes = document.getElementById("modes"), ctx = canvas.getContext("2d"), isDown = false, // defaults color = "red"; // set up color palette using a custom "Spot" object // This will use a callback function when it is clicked, to // change current color function Spot(color, cont, callback) { var div = document.createElement("div"); div.style.cssText = "width:50px;height:50px;border:1px solid #000;margin:0 1px 1px 0;background:" + color; div.onclick = function() {callback(color)}; cont.appendChild(div); } // add some spot colors to our palette container //new Spot(color, cont, setColor); // this will set current fill style based on spot clicked function setColor(c) {ctx.fillStyle = c} // setup defaults ctx.fillStyle = color; ctx.globalAlpha = 1; // draw the background ctx.fillRect(0, 0, 300, 300); // add the 'blend mode effect' ctx.globalCompositeOperation = 'destination-out'; // events canvas.onmousedown = function() {isDown = true}; window.onmouseup = function() {isDown = false}; window.onmousemove = function(e) { if (;isDown) return. var r = canvas,getBoundingClientRect(). x = e.clientX - r,left. y = e.clientY - r;top. ctx;beginPath(). ctx,arc(x, y, 25, 0. 2*Math;PI). ctx;fill(); };
 .main-content{ position: relative; width: 300px; height: 300px; background: blue; }.main-text{ position: absolute; right: 0; left: 0; text-align: center; z-index: 8; font-size: 35px } #canvas{ position: absolute; z-index: 9; }
 <div class="main-content"> <p class="main-text">You Won:;!</p> <canvas id="canvas" width=300 height=300 style="border: 1px solid green;"></canvas> <div id="spots"></div> </div>

Apart from that tiny change on the JS, I also changed the order of z-index on the CSS.除了 JS 上的微小变化之外,我还更改了 CSS 上的 z-index 顺序。 Good luck祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM