- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 html5 canvas 新手。我正在尝试在 Canvas 上制作一个盒子。我想要在这个盒子里面有一条垂直线(高度等于盒子高度)。我希望这条线在水平方向移动,比如在暂停和播放按钮上。我做不到。有人可以帮我吗?
我编写了在框中制作垂直线的代码。
class App extends Component {
componentDidMount(){
canvas = document.getElementsByTagName("canvas")[0];
ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(0,200);
ctx.stroke();
ctx.fillStyle = "#808891";
}
render() {
return (
<div className="App">
<canvas id="DemoCanvas" width="500" height="200"></canvas>
</div>
);
}
}
但我不知道如何使这条线在水平方向移动(将其视为视频时间轴的标记)
最佳答案
您需要创建一个函数,该函数将首先清理之前绘制的线条,然后在特定的 X 位置绘制新线条。
然后,每次需要时使用新的 X 位置调用此函数,例如 requestAnimationFrame。
这是带有播放/暂停按钮示例的代码片段。
var left = 0;
const btn = document.getElementById("btn");
canvas = document.getElementsByTagName("canvas")[0];
ctx = canvas.getContext("2d");
const w = canvas.width;
const h = canvas.height;
let xPos = 0;
let playing = false;
function updateVert() {
if(!playing) {
cancelAnimationFrame(updateVert);
return;
}
xPos += 1;
if(xPos >= w) {
// stop animation...
xPos = 0;
playing = false;
btn.value = "Play";
}
// reset rectangle content to erase previous line...
ctx.clearRect(0, 0, w, h);
// draw new one...
ctx.beginPath();
ctx.strokeStyle = "#19f";
ctx.lineWidth = 2;
ctx.moveTo(xPos, 0);
ctx.lineTo(xPos, 200);
ctx.stroke();
if(playing) requestAnimationFrame(updateVert);
}
document.getElementById("btn").addEventListener('click', function(e) {
e.preventDefault();
if(playing) {
// pause...
playing = false;
}
else {
playing = !playing;
updateVert();
}
btn.value = playing?"Pause":"Play";
});
canvas {
border: 1px solid silver;
}
<div className="App">
<canvas id="DemoCanvas" width="200" height="80"></canvas>
</div>
<input type="button" id="btn" value="Play">
关于javascript - 在框内水平移动垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51948524/
我正在尝试将按钮和 div 底部的一些文本对齐,就像下面的示例中的价格和 checkout 按钮一样。最好的方法是什么。我制作了一个 div,对其设置了样式以获得正确的文本和图片。我只需要将按钮附加到
将内容放置在带圆 Angular 的框 div 中以使其不会重叠的正确方法是什么? 蓝框是白框父div 里面的内容div。我希望标题位于该父框内,以便它在顶部也有圆 Angular 。 当我尝试“溢出
让 SVG 内联 HTML 文档,带有一个 View 框“x y w h”。如何检查元素是否在 View 框内? 通过说元素,它可以是组内的嵌套子路径等等...... 所以我想我正在寻找 SVG 的内
有没有办法检查鼠标是否在屏幕上的某个框内。我不知道将 jQuery 变量导出为整数以在 Javascript if 语句 中使用。我知道这可以通过 div 实现,但我不想使用 div 因为我需要很多这
目前我有两个带文本的 div 框 1 和 2,在第三个 div 框内将它们包含在一起。 Sometimes text in box 1 is longer than text inside box2
我是一名优秀的程序员,十分优秀!