话不多说直接上代码
<!doctype html><html><head> <meta charset="utf-8"> <title>title</title> </head> <body style="height:760px"> <canvas id="canvas" style="border:0px red solid;display:none"> </canvas> </body> </html>
因为项目需求,该动画中需要显示即时更新的数据,所以和普通的canvas画出来的不一样。但是又不能直接把html画到canvas中去,别着急有办法。
为了绘制 html 内容,你要先用<foreignobject> 元素包含 html 内容,然后再将这个 svg 图像绘制到你的 canvas 中。
夸张地说,这里唯一真正棘手的事情就是创建 svg 图像。您需要做的所有事情是创建一个包含xml 字符串的 svg,然后根据下面的几部分构造一个 blob。
blob 对象的 mime 应为 image/svg+xml。
一个 <svg> 元素。
在 svg 元素中包含的 <foreignobject> 元素。
包裹到 <foreignobject> 中的(格式化好的) html。
如上所述,通过使用一个 object url,我们可以内联 html 而不是从外部源加载它。当然,只要域与原始文档相同(不跨域),您也可以使用外部源。
//创建画布 var cans=document.getelementbyid(canvas); var ctx=cans.getcontext(2d); //设置全屏画布 cans.width=document.body.offsetwidth; cans.height=document.body.offsetheight; var domurl,img,svg,url; initimg(aaa);//默认显示数据,一下代码参考https://developer.mozilla.org/zh-cn/docs/web/api/canvas_api/drawing_dom_objects_into_a_canvas function initimg(data) { var data = '<svg xmlns="http://www.w3.org/2000/svg" width="52" height="52">' + '<foreignobject width="100%" height="100%">' + '<p xmlns="http://www.w3.org/1999/xhtml" style="font-size:12px">' + '<p style="width:50px;height:50px;border:1px red solid">' + ''+data+'</p>' + '</p>' + '</foreignobject>' + '</svg>'; domurl = window.url || window.webkiturl || window; img = new image(); svg = new blob([data], {type: 'image/svg+xml;charset=utf-8'}); url = domurl.createobjecturl(svg); img.src = url; } //每隔五秒刷新数据,随机从数组中取(实际情况当然是要从后台获取) var getdata = setinterval(function () { var data=[bbb,ccc,ddd,eee] initimg(data[math.floor(math.random()*8)]); },5000)
以下便是控制动画的显示位置和触发动画及关闭动画
var raf; var arror = []; var running = false; //绘制图形 function createstar() { return { x: 0, y: 0, vx: 0.7, vy: 0.7,//用来控制移动速度 draw: function() { ctx.drawimage(img, this.x, this.y); domurl.revokeobjecturl(url); } } } //清除 function clear() { ctx.fillstyle = 'rgba(255,255,255,1)'; ctx.fillrect(0,0,canvas.width,canvas.height); } //判断图形坐标是否超出画布范围 function draw() { clear(); arror.foreach(function(ball, i){ ball.draw(); ball.x += ball.vx; ball.y += ball.vy; if (ball.y + ball.vy+50 > canvas.height || ball.y + ball.vy < 0) { ball.vy = -ball.vy; } if (ball.x + ball.vx+50 > canvas.width || ball.x + ball.vx < 0) { ball.vx = -ball.vx; } }); raf = window.requestanimationframe(draw); } canvas.addeventlistener('click',function (e) { event.preventdefault(); window.cancelanimationframe(raf); if(!running){ cans.style.display=none document.onmousemove = document.onkeydown = document.onclick = null; cleartimeout(timer); clearinterval(getdata); clear(); }else{ running = false; bindevent(1); } }) function loadpi() { if (!running) { raf = window.requestanimationframe(draw); running = true; } var ball; ball = createstar(); ball.x = canvas.width/2-25; ball.y = canvas.height/2-25; arror.push(ball); document.onmousemove = document.onkeydown = document.onclick = null; cleartimeout(timer); } var timer; function bindevent(it) { cleartimeout(timer); timer = settimeout(function () { if(it==1){ raf = window.requestanimationframe(draw); running = true; }else{ cans.style.display=block loadpi(); } }, 3000); } window.onload = document.onmousemove = document.onkeydown = document.onclick = function () { bindevent(); }
相关推荐:
基于html5 canvas和rebound动画的loading加载动画特效
用h5的canvas做恐怖动画
canvas与js实现动态时钟动画
以上就是html5中canvas屏保动画的实现代码的详细内容。
