使用javascript绘制动画线(没有Raphael或其他插件)
Drawing an animated line using javascript (without Raphael or other plugins)
我正在尝试在SVG元素内绘制动画线。这条线是在一段时间内绘制的。
我搜索过,但是所有的答案通常都指向拉斐尔图书馆。但是,我不能使用互联网上提供的任何库。
需要一些指示从哪里开始
我从来没有在我的生活中使用过SVG,但在快速谷歌后的10分钟内,我想到了:
<svg width=200 height=200>
<line id="myLine" x1="5" y1="10" x2="5" y2="10" stroke-width=".5" stroke="red"/>
</svg>
<script>
var line = document.getElementById('myLine');
var count = 0;
var interval = window.setInterval(function() {
line.setAttribute('y2', 2 + +line.getAttribute('y2'));
line.setAttribute('x2', 1 + +line.getAttribute('x2'));
if (count++ > 75)
window.clearInterval(interval);
}, 100);
</script>
见:http://jsfiddle.net/YSmDH/
您应该使用<canvas id='mycanvas' width='300' height='300' />
元素并像这样画线:
function drawShape(){
// get the canvas element using the DOM
var canvas = document.getElementById('mycanvas');
// Make sure we don't execute when canvas isn't supported
if (canvas.getContext){
// use getContext to use the canvas for drawing
var ctx = canvas.getContext('2d');
// Stroked triangle
ctx.beginPath();
ctx.moveTo(125,125);
ctx.lineTo(125,45);
ctx.lineTo(45,125);
ctx.closePath();
ctx.stroke();
}
}
通过添加超时和清除2D-Context,然后创建新的,你可以动画你的行
这是一个非常好的关于画布操作的教程相关文章:
- 如何添加浮动和非浮动,其他
- 与其他库的jQuery.noConflict()
- 播放当前视频时如何停止其他视频?JavaScript
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- 除修剪外的其他功能
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- Array.length似乎不起作用;console.log则显示其他情况
- 如何消除代码中的未定义和其他问题
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- Jquery未定义函数正在停止其他操作
- 如何将我的javascript库公开给其他客户端使用
- Javascript阻止其他Javascript代码
- chart.series[id].remove()无法刷新高位图表/高位股票中其他系列的图例属性
- 缩放Raphael/SVG容器以适应所有内容
- 如何“;过滤器”;或者以其他方式重构该数据
- Raphael悬停函数没有'与其他潜水艇在一起时不起作用
- 使用javascript绘制动画线(没有Raphael或其他插件)