首页 > web前端 > H5教程 > 正文

在HTML5中怎样实现Canvas阴影效果

PHP中文网
发布: 2016-05-17 09:08:09
原创
1547人浏览过

我们知道现在html5的canvas阴影也经常使用的,这个就是html5 canvas阴影使用方法,在这里主要和大家分html5 canvas阴影使用方法代码,可以适当的改变来达到自己想要的结果,下面就一起来看看详细的代码吧!

1132.jpg

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=IE8">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Canvas Clip Demo</title>
<link href="default.css" rel="stylesheet" />
    <script>
        var ctx = null; // global variable 2d context
        var imageTexture = null;
        window.onload = function() {
            var canvas = document.getElementById("text_canvas");
            console.log(canvas.parentNode.clientWidth);
            canvas.width = canvas.parentNode.clientWidth;
            canvas.height = canvas.parentNode.clientHeight;
             
            if (!canvas.getContext) {
                console.log("Canvas not supported. Please install a HTML5 compatible browser.");
                return;
            }
            var context = canvas.getContext('2d');
             
            // section one - shadow and blur
            context.fillStyle="black";
            context.fillRect(0, 0, canvas.width, canvas.height/4);
            context.font = '60pt Calibri';
             
            context.shadowColor = "white";
            context.shadowOffsetX = 0;
            context.shadowOffsetY = 0;
            context.shadowBlur = 20;
            context.fillText("Blur Canvas", 40, 80);
            context.strokeStyle = "RGBA(0, 255, 0, 1)";
            context.lineWidth = 2;
            context.strokeText("Blur Canvas", 40, 80);
             
            // section two - shadow font
            var hh = canvas.height/4;
            context.fillStyle="white";
            context.fillRect(0, hh, canvas.width, canvas.height/4);
            context.font = '60pt Calibri';
             
            context.shadowColor = "RGBA(127,127,127,1)";
            context.shadowOffsetX = 3;
            context.shadowOffsetY = 3;
            context.shadowBlur = 0;
            context.fillStyle = "RGBA(0, 0, 0, 0.8)";
            context.fillText("Blur Canvas", 40, 80+hh);
             
            // section three - down shadow effect
            var hh = canvas.height/4 + hh;
            context.fillStyle="black";
            context.fillRect(0, hh, canvas.width, canvas.height/4);
            for(var i = 0; i < 10; i++)
            {
                context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
                context.shadowOffsetX = i*2;
                context.shadowOffsetY = i*2;
                context.shadowBlur = i*2;
                context.fillStyle = "RGBA(127, 127, 127, 1)";
                context.fillText("Blur Canvas", 40, 80+hh);
            }
             
            // section four -  fade effect
            var hh = canvas.height/4 + hh;
            context.fillStyle="green";
            context.fillRect(0, hh, canvas.width, canvas.height/4);
            for(var i = 0; i < 10; i++)
            {
                context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
                context.shadowOffsetX = 0;
                context.shadowOffsetY = -i*2;
                context.shadowBlur = i*2;
                context.fillStyle = "RGBA(127, 127, 127, 1)";
                context.fillText("Blur Canvas", 40, 80+hh);
            }
            for(var i = 0; i < 10; i++)
            {
                context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
                context.shadowOffsetX = 0;
                context.shadowOffsetY = i*2;
                context.shadowBlur = i*2;
                context.fillStyle = "RGBA(127, 127, 127, 1)";
                context.fillText("Blur Canvas", 40, 80+hh);
            }
            for(var i = 0; i < 10; i++)
            {
                context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
                context.shadowOffsetX = i*2;
                context.shadowOffsetY = 0;
                context.shadowBlur = i*2;
                context.fillStyle = "RGBA(127, 127, 127, 1)";
                context.fillText("Blur Canvas", 40, 80+hh);
            }
            for(var i = 0; i < 10; i++)
            {
                context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
                context.shadowOffsetX = -i*2;
                context.shadowOffsetY = 0;
                context.shadowBlur = i*2;
                context.fillStyle = "RGBA(127, 127, 127, 1)";
                context.fillText("Blur Canvas", 40, 80+hh);
            }
        }
         
    </script>
</head>
<body>
    <h1>HTML5 Canvas</h1>
    <pre class="brush:php;toolbar:false">Fill And Stroke Clip
登录后复制
    
             
HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号