gpt4 book ai didi

javascript - 在单个页面上绘制多个 Canvas

转载 作者:行者123 更新时间:2023-11-30 17:26:51 25 4
gpt4 key购买 nike

我需要在单个页面上绘制多个 Canvas ,如下所示:

<canvas id="myCanvas_1" width="71" height="80"></canvas>

<canvas id="myCanvas_2" width="71" height="80"></canvas>

<canvas id="myCanvas_3" width="71" height="80"></canvas>

我在 id "myCanvas_1"处绘制 Canvas 的脚本是:

<script>

var canvas = document.getElementById('myCanvas1');

var context = canvas.getContext('2d');
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 75;
var startAngle = 1.5 * Math.PI;
var endAngle = 0.6 * Math.PI;
var counterClockwise = false;

context.beginPath();
context.arc(x, y, radius, startAngle, endAngle, counterClockwise);
context.lineWidth = 150;

// line color
context.strokeStyle = 'black';
context.stroke();


</script>

我如何“循环”此脚本以在 id“myCanvas_2”、id“myCanvas_3”等上绘制 Canvas ...?

感谢大家

最佳答案

尝试一个js,

例如:

for(i = 1 ; i<4 ; i++){

var canvas = document.getElementById('myCanvas_'+i);
var context = canvas.getContext('2d');
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 75;
var startAngle = 1.5 * Math.PI;
var endAngle = 0.6 * Math.PI;
var counterClockwise = false;

context.beginPath();
context.arc(x, y, radius, startAngle, endAngle, counterClockwise);
context.lineWidth = 150;

// line color
context.strokeStyle = 'black';
context.stroke();
}

希望对你有帮助。

关于javascript - 在单个页面上绘制多个 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24080086/

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