`
sillycat
  • 浏览: 2579098 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

HTML5(四) colorful world

    博客分类:
  • UI
阅读更多
HTML5(四) colorful world
1. basic color
stokeStyle is for line
fillStyle is for area

statement bellows are the same color.
ctx.fillStyle = "orange";
ctx.fillStyle = "#FFA500";
ctx.fillStyle = "rgb(255,165,0)";
ctx.fillStyle = "rgba(255,165,0,1)";

sample for 36 circles, test4.html:
<canvas id="canvas1" width="150" height="150" style=" background-color: black">
you are out!
</canvas>
<br/>
<input type="button" value="fillStyle" onclick="fillStyle();" />
<input type="button" value="strokeStyle" onclick="strokeStyle();" />

<script type="text/javascript">
    function fillStyle() {
        var canvas = document.getElementById("canvas1");
        var ctx = canvas.getContext("2d");
        //clear the canvas
        ctx.clearRect(0,0,150,150);
       
        for (var i=0;i<6;i++){
            for (var j=0;j<6;j++){
                //set color values
                ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' +Math.floor(255-42.5*j) + ',0)';
                //begin paint
                ctx.beginPath();
                //draw circle and i,j for the center of the circle
                ctx.arc(12.5+j*25,12.5+i*25,10,0,Math.PI*2,true);
                ctx.fill();
            }
        }
    }

    function strokeStyle() {
        var canvas = document.getElementById("canvas1");
        var ctx = canvas.getContext("2d");
       
        ctx.clearRect(0,0,150,150);
        for (var i=0;i<6;i++){
            for (var j=0;j<6;j++){
                ctx.strokeStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' +Math.floor(255-42.5*j) + ',0)';
                ctx.beginPath();
                ctx.arc(12.5+j*25,12.5+i*25,10,0,Math.PI*2,true);
                ctx.stroke()
            }
        }
    }
</script>


2.Transparency

sample codes in file test4-1.html:
<canvas id="canvas2" width="150" height="150" style="background-position: center center;background-image:url(banner1.gif)">
    you are out!
</canvas>
<br />
Transparency<br />
<input type="button" value="0" onclick="alphaTest1(0);" />
<input type="button" value="0.2" onclick="alphaTest1(0.2);" />
<input type="button" value="0.4" onclick="alphaTest1(0.4);" />
<input type="button" value="0.6" onclick="alphaTest1(0.6);" />
<input type="button" value="0.8" onclick="alphaTest1(0.8);" />
<input type="button" value="1" onclick="alphaTest1(1);" />

<script type="text/javascript">
    function alphaTest1(alpah) {
        var canvas = document.getElementById("canvas2");
        var ctx = canvas.getContext("2d");
       
        ctx.clearRect(0,0,150,150);
       
        ctx.fillStyle="rgba(0,0,255,"+alpah+")"
        ctx.fillRect(20, 20, 110, 110)
    }
</script>

There is a property named globalAlpha, it is the controller of global alpha value.
my example file test4-2.html:
<canvas  id="canvas3" width="150" height="150" style="background-position: center center;background-image:url(banner2.gif)">
    you are out!
</canvas><br/>
globalalpha:
<input type="button" value="0" onclick="alphaTest2(0);" />
<input type="button" value="0.2" onclick="alphaTest2(0.2);" />
<input type="button" value="0.4" onclick="alphaTest2(0.4);" />
<input type="button" value="0.6" onclick="alphaTest2(0.6);" />
<input type="button" value="0.8" onclick="alphaTest2(0.8);" />
<input type="button" value="1" onclick="alphaTest2(1);" />

<script type="text/javascript">
    function alphaTest2(alpah){
        var canvas = document.getElementById("canvas3");
        var ctx = canvas.getContext("2d");
        ctx.clearRect(0,0,150,150);
        //set globalalpha value
        ctx.globalAlpha=alpah
       
        ctx.fillStyle="red"
        ctx.fillRect(10, 10, 30, 30)
        ctx.fillStyle="green"
        ctx.fillRect(10, 50, 30, 30)
        ctx.fillStyle="blue"
        ctx.fillRect(10, 90, 30, 30)
        ctx.fillStyle="yellow"
        ctx.beginPath()
        ctx.arc(100, 75, 40, 0, 360)
        ctx.fill()
    }
</script>

3.gradients
we have 2 methods to create a object named canvasGradient.
one method: createLinearGradient(x1,y1,x2,y2)
       (x1,y1) begin point
                   (x2,y2) end point

createRadialGradient(x1,y1,r1,x2,y2,r2)
(x1,y1) r1 first circle
(x2,y2) r2 second circle

addColorStop(position,color)
position between 0.0 to 1.0

my test file test4-3.html:
<canvas  id="canvas4" width="150" height="150" style=" background-color: black">
    you are out!
</canvas>
<br/>
gradient:
<input type="button" value="horizontal" onclick="gradients1();" />
<input type="button" value="vertical" onclick="gradients2();" />
<input type="button" value="bias" onclick="gradients3();" />
<input type="button" value="mutation" onclick="gradients4();" />
<input type="button" value="CircleOne" onclick="gradients5();" />
<input type="button" value="CircleTwo" onclick="gradients6();" />

<script type="text/javascript">
    function gradients1() {
        var ctx = document.getElementById('canvas4').getContext('2d');
        ctx.clearRect(0,0,150,150);
        //create line gradient object
        var lingrad = ctx.createLinearGradient(0,0,150,0);
        //add color
        lingrad.addColorStop(0, 'blue');
        lingrad.addColorStop(0.5, 'green');
        lingrad.addColorStop(1, 'white');
        ctx.fillStyle = lingrad;
        ctx.fillRect(10,10,130,130);
    }
    function gradients2() {
        var ctx = document.getElementById('canvas4').getContext('2d');
        ctx.clearRect(0,0,150,150);
        //create line
        var lingrad = ctx.createLinearGradient(0,0,0,150);
       
        lingrad.addColorStop(0, 'blue');
        lingrad.addColorStop(0.4, 'green');
        lingrad.addColorStop(1, 'white');
        ctx.fillStyle = lingrad;
        ctx.fillRect(10,10,130,130);
    }
    function gradients3() {
        var ctx = document.getElementById('canvas4').getContext('2d');
        ctx.clearRect(0,0,150,150);
        var lingrad = ctx.createLinearGradient(0,0,150,150);
        lingrad.addColorStop(0, 'blue');
        lingrad.addColorStop(0.5, 'green');
        lingrad.addColorStop(1, 'white');
        ctx.fillStyle = lingrad;
        ctx.fillRect(10,10,130,130);
    }
    function gradients4() {
        var ctx = document.getElementById('canvas4').getContext('2d');
        ctx.clearRect(0,0,150,150);
        var lingrad = ctx.createLinearGradient(0,0,0,150);
        lingrad.addColorStop(0, 'blue');
        lingrad.addColorStop(0.5, 'white');
        lingrad.addColorStop(0.5, 'green');
        lingrad.addColorStop(1, 'white');
        ctx.fillStyle = lingrad;
        ctx.fillRect(10,10,130,130);
    }
    function gradients5() {
        var ctx = document.getElementById('canvas4').getContext('2d');
        ctx.clearRect(0,0,150,150);
        var lingrad = ctx.createRadialGradient(75,75,10,75,75,70);
        lingrad.addColorStop(0, 'white');
        lingrad.addColorStop(1, 'rgba(255,255,255,0)');
        ctx.fillStyle = lingrad;
        ctx.arc(75, 75, 70, 0, 360);
        ctx.fill();
    }
    function gradients6() {
        var ctx = document.getElementById('canvas4').getContext('2d');
        ctx.clearRect(0,0,150,150);
        var lingrad = ctx.createRadialGradient(5,5,10,75,75,70);
        lingrad.addColorStop(0, 'white');
        lingrad.addColorStop(1, 'rgba(255,255,255,0)');
        ctx.fillStyle = lingrad;
        ctx.arc(75, 75, 70, 0, 360);
        ctx.fill();
    }
</script>

4.shadow

shadowOffsetX = float
shadowOffsetY = float
shadowBlur = float
shadowColor = color

shadowOffsetX and shadowOffsetY are the shadow distance of x and y;
shadowBlur is 0-1

my example file test4-4.html:
<canvas  id="canvas5" width="150" height="150" style=" background-color: black">
    you are out!
</canvas><br/>
<input type="button" value="pic shadow" onclick="shadow1();" />
<input type="button" value="txt shadow" onclick="shadow2();" />

<script type="text/javascript">
    function shadow1() {
        var ctx = document.getElementById('canvas5').getContext('2d');
        ctx.clearRect(0,0,150,150);
        ctx.shadowOffsetX = 5;
        ctx.shadowOffsetY = 5;
        ctx.shadowBlur    = 4;
        ctx.shadowColor   = 'rgba(255, 0, 0, 0.5)';
        ctx.fillStyle     = 'blue';
        ctx.fillRect(10, 10, 130, 130);
    }

    function shadow2() {
        var ctx = document.getElementById('canvas5').getContext('2d');
        ctx.clearRect(0,0,150,150);
        ctx.shadowOffsetX = 4;
        ctx.shadowOffsetY = 3;
        ctx.shadowBlur = 2; 
        ctx.shadowColor = "rgba(255, 255, 255, 0.5)";

        ctx.font = "20px Times New Roman";
        ctx.fillStyle = "red";
        ctx.fillText("Sample String", 15, 70);
    }
</script>

references:
http://www.blogjava.net/myqiao/category/46360.html

分享到:
评论

相关推荐

    colorful一键恢复

    5. **设置工具**:可能包含用于配置恢复选项的工具,比如设置自动备份频率,或者选择仅恢复操作系统而不包括个人文件。 6. **许可证和授权文件**:为了合法地恢复预装软件,可能需要的授权文件和密钥。 使用...

    Colorful FX

    5. **性能优化**:考虑到游戏性能的重要性,Colorful FX在提供丰富特效的同时,也注重性能优化,确保在各种设备上都能流畅运行。 6. **资源管理**:通过方便的资源管理系统,开发者可以轻松地管理和导入/导出特效...

    VS背景及环境插件Colorful-IDE

    5. **性能优化**:尽管增加了视觉效果,但Colorful IDE努力保持低内存占用和流畅的性能,确保不影响开发速度。 6. **兼容性**:Colorful IDE通常会支持多个Visual Studio版本,确保用户在升级VS后仍能继续使用熟悉...

    Android-Colorful一个动态主题库

    5. 提供用户接口:为了让用户能够选择和切换主题,你需要创建一个用户界面,展示可用的主题选项,并监听用户的颜色选择。当用户选择新颜色后,调用Colorful提供的API来切换当前主题。 Colorful库的优势在于其灵活性...

    Android代码-Colorful

    Colorful Colorful is a dynamic theme library allowing you to change your apps' color schemes easily. License Colorful is licensed under the Apache 2.0 License, in common Android style. Copyright 2016...

    colorful_js_进度条_

    5. `js`:这可能是一个包含其他JavaScript文件的文件夹,如果项目中有分离的模块化代码,它们可能在这里。 6. `colorful`:这个文件名不太明确,但根据上下文,可能是与进度条颜色配置相关的文件或者一个包含多种...

    colorful1.2-net-work(Colorful1.2-网卡接口信息插件)

    Java精准 'get' MAC/IP4/IP6/子网掩码/网关/适配器/计算机信息colorful1.2框架插件 (Windows操作系统提供的数据API上获取并且写成Java插件) 你可以通过:http://t.csdn.cn/RGeGr文章来查看教程使用方式

    colorful-network.ini

    SecureCRT配色方案源代码-colorful-network.ini。终端有一个好的配色,不仅能保护自己的眼睛,也能给人一个好心情,本配色方案适合任意一种SSH客户端软件。

    SecureCRT配置颜色添加colorful-network脚本

    SecureCRT配置颜色添加colorful-network脚本

    colorful颜色截取小工具

    为了适应这一需求,“colorful颜色截取小工具”应运而生。这一小巧实用的工具,彻底改变了我们获取屏幕上颜色的方式,极大地提高了用户在进行颜色相关工作时的效率和便利性。 “colorful颜色截取小工具”采用了极为...

    CRT8.0 colorful-network.zip

    在CRT8.0的基础上开发的“CRT8.0 colorful-network.zip”代码包,更是进一步优化了用户体验,特别是在代码阅读和远程终端操作方面。 通过“CRT8.0 colorful-network.zip”这个代码包,用户可以享受到在CRT环境下...

    Unity Colorful FX4.2.2.unitypackage

    Unity Colorful FX4.2.2.unitypackage

    Colorful FX v4.2.2.unitypackage

    unity3d Colorful FX是一款快速和可定制的后期处理效果和颜色调整的统一集合工具。(unity十大热门工具) 些资源来自unity商店,仅限于个人学习研究,请勿用于任何商业用途,请支持或购买正版,尊重版权.

    动态主题库Colorful,容易地改变App的配色方案

    5. **实践案例** - **实例应用**:可以通过查看博文链接(https://gundumw100.iteye.com/blog/2347577)来了解如何在实际项目中应用Colorful,文章可能包含详细的步骤和示例代码。 - **优化建议**:在使用过程中,...

    JavaScript背景颜色定制包colorful.js.zip

    colorful.js 能让 HTML 元素具有自定义的颜色变幻背景。 colorful.js 通过用户自定义关键帧的颜色,使用css3实现颜色渐变功能。在低版本浏览器会通过javascript来实现。在线演示最简单用法:startLoop(document....

    Colorful Sea-Fish Pack v1.1

    《Colorful Sea-Fish Pack v1.1》是一套彩色海洋鱼类资源包,包含了多种色彩斑斓的海底生物模型。此类资源包通常用于游戏开发、3D建模或虚拟现实等场景,为开发者提供了一套丰富的海洋鱼类素材。文件中包含的“gzh....

    SecureCRT Version9.0.2 配置颜色添加colorful-network脚本

    1. 首先在工具栏找到Options-&gt;...2. 在工具栏找到Options-&gt;Global Options-&gt;Default Session 点击Edit Default Setting,然后再选Keyword Highlighting选择colorful-network配置文件即可。 3.我的CRT是Version 9.0.2

    colorful-uni-uniapp资源

    标题“colorful-uni-uniapp资源”暗示这是一个集合了多彩元素的uni-app资源包。uni-app是一个使用Vue.js开发所有前端应用的框架,能够编译到iOS、Android、以及各种小程序等多个平台。它允许开发者编写一次代码,...

    Colorful - Image Effects v4.1 - 相机调色插件

    Colorful - Image Effects v4.1 是一款专为Unity 5.x平台设计的相机调色插件,它为开发者和艺术家提供了丰富的图像调整工具,旨在提升游戏或应用中的视觉体验,让每个场景都充满生动的色彩和深度。 Unity 5.x版本的...

    Unity Colorful FX

    Unity Colorful FX 希望对开发者有用,资源官方地址:https://assetstore.unity.com/packages/vfx/shaders/fullscreen-camera-effects/colorful-fx-44845

Global site tag (gtag.js) - Google Analytics