gpt4 book ai didi

javascript - 如何在 Canvas 周围绘制边框

转载 作者:搜寻专家 更新时间:2023-10-31 08:27:59 25 4
gpt4 key购买 nike

我正在用白色背景颜色在 Canvas 上绘制图像。我想在 Canvas 周围画一个边框,但我做不到。这是我的代码:

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

在此之后,我希望在整个 Canvas 周围出现一个红色边框。

最佳答案

context.lineWidth设置为2,将context.strokeStyle设置为#FF0000",并使用context.strokeRect,而不是 fillRectglobalCompositeOperation 如果设置为destination-over,那么新应用的东西将使用 Canvas 的值,所以更改为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>

关于javascript - 如何在 Canvas 周围绘制边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31045689/

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