HTML5   发布时间:2022-04-25  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了HTML5 canvas大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

简介

HTML5 <canvas> 是绘制 图形/图像 的容器。它本身并没有绘制能力,您必须使用脚本来完成实际的绘图任务(通常是 JavaScript)。

如果浏览器不支持<canvas>的话,会显示lt;canvas>标签内定义的元素。例如,下面代码不支持的<canvas>浏览器下就会忽略<canvas>标签显示<p>标签

<canvas id="canvas" width="500" height="300" style="border:1px solid #aaa">
    <p>Your browser does not support the canvas element!</p>  
</canvas>

JavaScript 获取 <canvas>元素

获取html中的 <canvas>元素

var canvas = document.getElementById("canvas");

创建绘图上下文对象

getContext(contextID) 方法可返回一个对象,该对象提供了用于在画布上绘图的方法属性。该方法需要一个参数,一般是2d,即getContext(‘2d’)。这是为了将来扩展到3d画布保留的一个参数。

var ctx = canvas.getContext('2d');

这样就可以在画布上下文中绘制图案了。

JavaScript 绘制基本图形

颜色

  • fillStyle
    设置或返回用于填充绘画的颜色、渐变或模式。
  • strokeStyle
    设置或返回用于笔触的颜色、渐变或模式。
<html>
    <body>
        <canvas id="canvas" width="250" height="100" style="border:1px solid #aaa">
            <p>Your browser does not support the canvas element!</p>  
        </canvas>
        <script type="text/javascript"> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.fillStyle='#f00'; ctx.fillRect(10,10,100,50); ctx.strokeStyle="#0f0" ctx.strokeRect(130,50); </script>
    </body>
</html>

HTML5 canvas

渐变

阴影

  • shadowColor
    设置或返回用于阴影的颜色。
  • shadowBlur
    设置或返回用于阴影的模糊级别。
  • shadowOffsetX
    设置或返回阴影与形状的水平距离,右为正方向。
  • shadowOffsetY
    设置或返回阴影与形状的垂直距离,下为正方向。
<html>
    <body>
        <canvas id="canvas" width="280" height="400" style="border:1px solid #aaa">
            <p>Your browser does not support the canvas element!</p>  
        </canvas>
        <script type="text/javascript"> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.shadowBlur=0; ctx.shadowColor="black"; ctx.fillStyle='#f00'; ctx.strokeStyle="#0f0" ctx.fillRect(10,50); ctx.strokeRect(130,50); ctx.shadowBlur=20; ctx.fillRect(10,50); ctx.shadowOffsetX=20; ctx.shadowOffsetY=20; ctx.fillRect(10,200,50); ctx.shadowBlur=0; ctx.fillRect(10,300,50); </script>
    </body>
</html>

HTML5 canvas

线条样式

矩形

路径

转换

文本

绘制图像

  • drawImage()
    向画布上绘制图像、画布或视频。
<html>
    <body>
        <img id="img" src="simayi.jpg" />
        <canvas id="canvas1" width="280" height="300" style="border:1px solid #aaa">
            <p>Your browser does not support the canvas element!</p>  
        </canvas>
        <canvas id="canvas2" width="280" height="300" style="border:1px solid #aaa;display:none"/>
        <script type="text/javascript"> var canvas2 = document.getElementById('canvas2'); var ctx2 = canvas2.getContext('2d'); ctx2.fillStyle="red"; ctx2.fillRect(0,0,260,50); var canvas1 = document.getElementById('canvas1'); var ctx1 = canvas1.getContext('2d'); var img = document.getElementById('img'); img.onload = function() { ctx1.drawImage(img,10); } ctx1.drawImage(canvas2,220); </script>
    </body>
</html>

HTML5 canvas

像素操作

合成

其它

大佬总结

以上是大佬教程为你收集整理的HTML5 canvas全部内容,希望文章能够帮你解决HTML5 canvas所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: