canvas教程

Html5 Canvas开发之清除一个特定区域内的Canvas、宽高技巧、使Ca

字号+ 作者:H5之家 来源:H5之家 2018-01-18 17:11 我要评论( )

1.清除一个特定区域内的Canvascontext.fillRect(40, 40, 100, 100);context.beginPath();context.arc(230, 90, 50, 0, Math.PI*2, false);context.closePath();c

1.清除一个特定区域内的Canvas

context.fillRect(40, 40, 100, 100); context.beginPath(); context.arc(230, 90, 50, 0, Math.PI*2, false); context.closePath(); context.fill(); context.clearRect(230, 90, 50, 50);


2.宽高技巧

context.fillStyle = "rgb(255, 0, 0)"; context.fillRect(40, 40, 100, 100); context.beginPath(); context.arc(230, 90, 50, 0, Math.PI*2, false); context.closePath(); context.fill(); canvas.attr("width", canvas.width()); canvas.attr("height", canvas.height()); context.fillRect(40, 40, 100, 100);
注意:

The downside with the width/height trick isthat absolutely everything in the canvas is reset, includingstyles and colors.This is why you should only use this trick if you!re prepared to completelyreset the canvas, not just wipe the display clean.


3.

使Canvas填充整个浏览器窗口

<!DOCTYPE html> <html> <head> <title>Learning the basics of canvas</title> <meta charset="utf-8"> <link href="canvas.css" type="text/css"> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function() { var canvas = $("#myCanvas"); var context = canvas.get(0).getContext("2d"); $(window).resize(resizeCanvas); function resizeCanvas() { canvas.attr("width", $(window).get(0).innerWidth); canvas.attr("height", $(window).get(0).innerHeight); context.fillRect(0, 0, canvas.width(), canvas.height()); }; resizeCanvas(); }); </script> </head> <body> <canvas> <!-- Insert fallback content here --> </canvas> </body> </html>
* { margin: 0; padding: 0; } html, body { height: 100%; width: 100%; } canvas { display: block; }
注意点:1.

If only jQuery had a resize method that was fired at the moment a browser window was resized。

2.

changing the width and height will reset the canvas, so everything has to be redrawn).




 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • HTML5 Canvas 在线参考手册,语法速查表

    HTML5 Canvas 在线参考手册,语法速查表

    2018-01-19 08:02

  • Tkinter教程之Canvas(1)篇

    Tkinter教程之Canvas(1)篇

    2018-01-18 16:05

  • 关于canvas的学习心得(一)

    关于canvas的学习心得(一)

    2018-01-18 13:16

  • HTML5 中用CANVAS画一个五角星

    HTML5 中用CANVAS画一个五角星

    2018-01-18 09:05

网友点评