HTML5中canvas实现矩形颜色渐变
2016-06-12 18:03
671 查看
<!--<!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前。--> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset = utf-8"> <title>HTML5</title> <script type="text/javascript" charset = "utf-8"> //这个函数将在页面完全加载后调用 window.onload = function() { draw(); }; function draw(grad) { var canvas = document.getElementById('c1'); if (!canvas||! canvas.getContext ) { return false; } var ctx = canvas.getContext('2d'); ctx.beginPath(); /* 指定渐变区域 */ var grad = ctx.createLinearGradient(0,0, 0,140); /* 指定几个颜色 */ grad.addColorStop(0,'rgb(216, 9, 0)'); // grad.addColorStop(0.5,'rgb(225, 255, 0)'); // 绿 grad.addColorStop(1,'rgb(42, 231, 0)'); //hong /* 将这个渐变设置为fillStyle */ ctx.fillStyle = grad; /* 绘制矩形 */ ctx.rect(10,10,20,140); ctx.fill(); // ctx.fillRect(0,0, 140,140); } </script> </head> <body> <canvas width = "400" height = "200" id = "c1" style = "border:black 1px solid;"> <!--如果浏览器不支持则显示如下字体--> 提示:你的浏览器不支持<canvas>标签 </canvas> </body> </html>
相关文章推荐
- h5开发中常见的问题汇总
- HTML+CSS3-》第2阶段:HTML5基础和HTML语义化 上
- 基于canvas的图片轮播(h5)
- ionic、cordova点击拨打电话的实现
- HTML5通用接口详解
- Web前端教程-02.04.HTML5样式、链接和表格
- Web前端教程-02.03.HTML5元素、属性、格式化
- Web前端教程-02.02.HTML5特性简介
- H5 canvas 笔记 一
- Web前端教程-02.01.HTML5开发前的准备
- HTML5中canvas画图之绘制数据图表
- 每天15min-HTML5(11)-表单(中)
- html5调用摄像头,录音功能
- H5调用讯飞语音接口实现在线语音听写测评
- HTML5与HTML4的区别
- HTML5学习之FileReader接口
- HTML5中canvas画图之绘制方格图
- HTML5:Canvas
- HTML5知识点总结
- HTML5程序的书写样式