《canvas》之第12章 其他应用

130 篇文章 4 订阅
20 篇文章 4 订阅

第12章 其他应用

12.1 canvas对象

document.getElementById()获取canvas对象。

12.1.1 canvas对象属性

width、height。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta charset="utf-8" />
    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
    <script type="text/javascript">
        function $$(id) {
            return document.getElementById(id);
        }
        window.onload = function () {
            var cnv = $$("canvas");
            var cxt = cnv.getContext("2d");

            //绘制初始图形
            cxt.fillStyle = "#FF6699";
            cxt.fillRect(30, 30, 50, 50);

            $$("btn").onclick = function () {
                cxt.clearRect(0, 0, cnv.width, cnv.height);
                cxt.translate(10, 10);
                cxt.fillStyle = "#FF6699";
                cxt.fillRect(30, 30, 50, 50);
            }
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas><br />
    <input id="btn" type="button" value="移动" />
</body>
</html>

canvas对象方法

getContext(“2d”),获取canvas 2d上下文环境对象。
toDataURL(),获取canvas对象产生的位图的字符串。

cnv.toDataURL(type);

type,可选参数,输出的MIME类型(type省略时,默认image/png)。
MIME类型,设定用一种应用程序来打开某种扩展名的文件的方式类型。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function $$(id) {
            return document.getElementById(id);
        }
        window.onload = function () {
            var cnv = $$("canvas");
            var cxt = cnv.getContext("2d");

            //定义文字
            var text = "绿叶学习网";
            cxt.font = "bold 60px 微软雅黑";

            //定义阴影
            cxt.shadowOffsetX = 5;
            cxt.shadowOffsetY = 5;
            cxt.shadowColor = "#66CCFF";
            cxt.shadowBlur = 10;

            //填充文字
            cxt.fillStyle = "#FF6699";
            cxt.fillText(text, 10, 90);

            $$("btn").onclick = function () {
            	//当前页面打开url链接
                window.location.href = cnv.toDataURL("image/png"); 
            }
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="320" height="150" style="border:1px dashed gray"></canvas><br />
    <input id="btn" type="button" value="保存图片" />
</body>
</html>

点击按钮,浏览器地址产生字符串:data:image/png;base64,iV.....==
toDataURL()方法将画布保存为Base64位编码的URL,可直接嵌入网页的小型数据,如img元素的图片文件等。
data URL用处:

  • 发送图片数据到web服务器的数据库,进行长期保存。
  • 浏览器中直接打开,进行本地保存。

12.2 globalAlpha属性

定义canvas环境的透明度。

cxt.globalAlpha = "数值";

取值范围0.0(完全透明)~1.0(完全不透明,默认值)。
针对整个canvas,想实现局部图形或文字的透明效果,可使用RGBA。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function $$(id) {
            return document.getElementById(id);
        }
        window.onload = function () {
            var cnv = $$("canvas");
            var cxt = cnv.getContext("2d");

            cxt.globalAlpha = "0.3";
            
            cxt.font = "20px bold 微软雅黑";
            
            cxt.fillStyle = "purple";
            cxt.fillText("绿叶学习网", 50, 40);

            cxt.fillStyle = "HotPink";
            cxt.fillRect(50, 50, 100, 50);
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>

12.3 globalCompositeOperation属性

设置交叉图形的显示方式。

cxt.globalCompositeOperation = "属性值";
属性值说明
source-over新图形覆盖旧图形重叠部分,默认值
source-in只显示新图形重叠部分,其它部分透明处理
source-out只显示新图形未重叠部分,其它部分透明处理
source-atop只显示旧图形+新图形重叠部分,其它部分透明处理
destination-over旧图形覆盖新图形重叠部分
destination-in只显示旧图形重叠部分,其它部分透明处理
destination-out只显示旧图形未重叠部分,其它部分透明处理
destination-atop只显示新图形+旧图形重叠部分,其它部分透明处理
copy只显示新图形,旧图形透明处理
xor两种图形都显示,重叠部分透明处理
darker两种图形都显示,重叠部分相减
lighter两种图形都显示,重叠部分相加
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function $$(id) {
            return document.getElementById(id);
        }
        window.onload = function () {
            var cnv = $$("canvas");
            var cxt = cnv.getContext("2d");

            //绘制矩形
            cxt.fillStyle = "HotPink";
            cxt.fillRect(30, 30, 60, 60);
            
            //绘制圆形
            cxt.beginPath();
            cxt.arc(100, 100, 40, 0, Math.PI * 2, true);
            cxt.closePath();
            
            cxt.globalCompositeOperation = "xor";
            cxt.fillStyle = "LightSkyBlue";
            cxt.fill();
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function $$(id) {
            return document.getElementById(id);
        }
        window.onload = function () {
            var cnv = $$("canvas");
            var cxt = cnv.getContext("2d");

            cxt.globalCompositeOperation = "xor";
			
            //绘制第1个矩形
            cxt.fillStyle = "HotPink";
            cxt.fillRect(30, 30, 60, 60);
			
			cxt.save();
            cxt.globalCompositeOperation = "xor";
            //绘制圆形
            cxt.beginPath();
            cxt.arc(100, 100, 40, 0, Math.PI * 2, true);
            cxt.closePath();
            cxt.fillStyle = "LightSkyBlue";
            cxt.fill();
			cxt.restore();
			
            //绘制第2个矩形
            cxt.fillStyle = "HotPink";
            cxt.fillRect(110, 30, 60, 60);
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>

12.4 strokeStyle和fillStyle

分别用于描边型stroke()和填充型fill()。

  • 矩形
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function $$(id) {
            return document.getElementById(id);
        }
        window.onload = function () {
            var cnv = $$("canvas");
            var cxt = cnv.getContext("2d");

            cxt.strokeStyle = "HotPink";
            cxt.strokeRect(20, 20, 50, 50);

            cxt.fillStyle = "LightSkyBlue";
            cxt.fillRect(100, 20, 50, 50);
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
  • 圆形
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function $$(id) {
            return document.getElementById(id);
        }
        window.onload = function () {
            var cnv = $$("canvas");
            var cxt = cnv.getContext("2d");

            cxt.beginPath();
            cxt.arc(50, 50, 25, 0, 360 * Math.PI / 180, false);
            cxt.closePath();
            cxt.strokeStyle = "HotPink";
            cxt.stroke();

            cxt.beginPath();
            cxt.arc(150, 50, 25, 0, 360 * Math.PI / 180, false);
            cxt.closePath();
            cxt.fillStyle = "LightSkyBlue";
            cxt.fill();
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
  • 文字
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function $$(id) {
            return document.getElementById(id);
        }
        window.onload = function () {
            var cnv = $$("canvas");
            var cxt = cnv.getContext("2d");

            var text = "绿叶学习网";
			
            cxt.font = "bold 25px 微软雅黑";
			
            cxt.strokeStyle = "purple";
            cxt.strokeText(text, 30, 50);

            cxt.fillStyle = "purple";
            cxt.fillText(text, 30, 100);
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
  • 图片
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function $$(id) {
            return document.getElementById(id);
        }
        window.onload = function () {
            var cnv = $$("canvas");
            var cxt = cnv.getContext("2d");

            //创建image对象
            var image = new Image();
            image.src = "princess.png";

            image.onload = function () {
                var text = "绿叶学习网";
                cxt.font = "bold 22px 微软雅黑";
                var pattern = cxt.createPattern(image, "no-repeat");
                cxt.fillStyle = pattern;
                cxt.fillText(text, 10, 50);
            }
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
  • princess.png
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值