gpt4 book ai didi

javascript - 如何在两个单元格之间的 Canvas 线上动态绘制图像

转载 作者:行者123 更新时间:2023-11-30 12:35:50 25 4
gpt4 key购买 nike

我知道如何使用 <canvas> 画线标签。
我想用我创建的 Canvas 线绘制梯子。
它应该是动态的,因为线的 Angular 每次都在变化,因此复制\粘贴具有特定 Angular 梯子图片的手动解决方案对我没有帮助。

这是我的:
enter image description here

这就是我想要的:
enter image description here

我尝试使用:

      var imageObj = new Image();

imageObj.onload = function() {
context.drawImage(imageObj, 120, 50);
};
imageObj.src = 'http://s28.postimg.org/7sjqjzpah/ladderpattern.png';

但它只打印了梯子的一小部分。
知道如何让创建的每一行都显示为阶梯吗?

DEMO (JSFIDDLE): Board with only lines (without ladders)
DEMO (JSFIDDLE): With picture I tried to draw

CSS:

td {
width: 100px;
height: 90px;
text-align: left;
vertical-align: top;
border: 1px solid black;
position: relative;
}
table
{
position: fixed;
left:9px;
top:8px;
}

tr:nth-child(even) td:nth-child(odd), tr:nth-child(odd) td:nth-child(even)
{
background:#00A2E8;
}

td span {
position: absolute;
bottom: 0;
}

#myCanvas {
z-index: 10;
position:absolute;
font:bold;
color:red;

}

HTML:

<body>

<div id="board" value="5">
<canvas id="myCanvas" width="600" height="500"></canvas>
<canvas id="myCanvas2" width="600" height="500"></canvas>
<table oncontextmenu="return false">
<tbody>
<tr>
<td class="" cellnumber="21" row="4" col="0"><span>21</span></td>
<td class="" cellnumber="22" row="4" col="1"><span>22</span></td>
<td class="" cellnumber="23" row="4" col="2"><span>23</span></td>
<td class="" cellnumber="24" row="4" col="3"><span>24</span></td>
<td class="" cellnumber="25" row="4" col="4"><span>25</span></td>
</tr>
<tr>
<td class="" cellnumber="16" row="3" col="0"><span>16</span></td>
<td class="" cellnumber="17" row="3" col="1"><span>17</span></td>
<td class="" cellnumber="18" row="3" col="2"><span>18</span></td>
<td class="" cellnumber="19" row="3" col="3"><span>19</span></td>
<td class="" cellnumber="20" row="3" col="4"><span>20</span></td></tr>
<tr>
<td class="" cellnumber="11" row="2" col="0"><span>11</span></td>
<td class="" cellnumber="12" row="2" col="1"><span>12</span></td>
<td class="" cellnumber="13" row="2" col="2"><span>13</span></td>
<td class="" cellnumber="14" row="2" col="3"><span>14</span></td>
<td class="" cellnumber="15" row="2" col="4"><span>15</span> </td>
</tr>
<tr>
<td class="" cellnumber="6" row="1" col="0"><span>6</span></td>
<td class="" cellnumber="7" row="1" col="1"><span>7</span></td>
<td class="" cellnumber="8" row="1" col="2"><span>8</span></td>
<td class="" cellnumber="9" row="1" col="3"><span>9</span></td>
<td class="" cellnumber="10" row="1" col="4"><span>10</span></td>
</tr>
<tr>
<td class="" cellnumber="1" row="0" col="0"><span>1</span>
<img src = "http://s23.postimg.org/ynlvim1x3/image.png" onclick="doSomething()"></td>
<td class="" cellnumber="2" row="0" col="1"><span>2</span></td>
<td class="" cellnumber="3" row="0" col="2"><span>3</span></td>
<td class="" cellnumber="4" row="0" col="3"><span>4</span></td>
<td class="" cellnumber="5" row="0" col="4"><span>5</span></td>
</tr>
</table>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var imageObj = new Image();

imageObj.onload = function() {
context.drawImage(imageObj, 120, 50);
};
imageObj.src = 'http://s28.postimg.org/7sjqjzpah/ladderpattern.png';

context.beginPath();
context.moveTo(120, 45);
context.lineTo(350, 150);
context.stroke();
context.stroke();

function doSomething()
{
alert("Ping");
}

最佳答案

您可以使用 Canvas 的转换来绘制您的斜梯:

  • 保存未旋转的上下文状态:context.save();

  • 翻译到行首:context.translate(x,y);

  • 旋转到与直线相同的 Angular :context.rotate(lineAngle);

  • Translate 在指定的 x,y 处重置 0,0 原点。 Rotate 会将整个 Canvas 旋转指定的 Angular ,旋转点位于您在 Translate 中指定的 x,y 位置。通过使用平移和旋转,您不必为每条新线重新计算绘制阶梯的代码坐标。

  • 画出你的梯子。

  • 将上下文恢复到其未旋转状态:context.restore();

enter image description here

这是示例代码和演示:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

drawLadder(25,25,150,125);

function drawLadder(x0,y0,x1,y1){
var dx=x1-x0;
var dy=y1-y0;

// calc the length of the line
var length=Math.sqrt(dx*dx+dy*dy);

// calc the angle of the line
var angle=Math.atan2(dy,dx);

// draw the ladder
ctx.save();
ctx.translate(x0,y0);
ctx.rotate(angle);
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(length,0);
ctx.moveTo(0,8);
ctx.lineTo(length,8);
for(var i=1;i<(length-2)/4;i++){
ctx.moveTo(i*4,0);
ctx.lineTo(i*4,8);
}
ctx.stroke();
ctx.restore();

}
body{ background-color: ivory; }
canvas{border:1px solid red;}
<canvas id="canvas" width=300 height=300></canvas>

关于javascript - 如何在两个单元格之间的 Canvas 线上动态绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26122861/

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