问题描述
我正在白色背景颜色的画布上绘制图像。我想在画布周围绘制边框,但无法这样做。这是我的代码:
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.
推荐答案
设置 context.lineWidth
设置为 2
,将 context.strokeStyle
设置为#FF0000
,并使用 context.strokeRect
,而不是 fillRect
。
globalCompositeOperation
如果设置为 destination-over
,则新应用的东西将使用canvas的值,因此更改为 source-over
。使用 lightblue
伪造代码中的 drawImage
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
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>
这篇关于如何在画布周围绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!