简体   繁体   English

如何在画布周围绘制边框

[英]How to draw border around canvas

I am drawing an image on a canvas with white background color. 我正在画布上用白色背景颜色绘制图像。 I want to draw a border around the canvas and I am unable to do so. 我想在画布周围画一个边框,我无法这样做。 Here is my code: 这是我的代码:

canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');
context.fillStyle = "black";
context.font = "50px Arial";
context.fillText(chartId, 0, 50);
context.drawImage(image, 0, 0);
context.globalCompositeOperation = "destination-over";
context.fillStyle = "#FFFFFF";
context.fillRect(0,0,canvas.width,canvas.height);//for white background

after this i want a red border to appear around the whole canvas. 在此之后我想在整个画布周围出现一个红色边框。

Set context.lineWidth to 2 , set context.strokeStyle to #FF0000" , and use context.strokeRect , not fillRect . globalCompositeOperation if set to destination-over , then the newly apply thing will use canvas's value, so change to source-over . Used lightblue to fake the drawImage in your code 设置context.lineWidth2 ,设置context.strokeStyle#FF0000" ,并使用context.strokeRect ,不fillRectglobalCompositeOperation如果设置为destination-over ,那么新申请的事情将使用画布的价值,所以更改为source-over 。使用lightblue伪造代码中的drawImage

 var canvas = document.getElementById('cv'); canvas.width = 400; canvas.height = 300; var context = canvas.getContext('2d'); context.fillStyle = "black"; context.font = "50px Arial"; context.fillText('ASD', 0, 50); context.globalCompositeOperation = "destination-over"; context.fillStyle = "#00FFFF"; context.fillRect(0,0,canvas.width,canvas.height);//for white background context.globalCompositeOperation = "source-over"; context.lineWidth = 2; context.strokeStyle="#FF0000"; context.strokeRect(0, 0, canvas.width, canvas.height);//for white background 
 <canvas id="cv"></canvas> 

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

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