• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >html5 > HTML5版径向渐变梯度色彩

HTML5版径向渐变梯度色彩

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-03

本文主要包含HTML5,梯度色彩,径向渐变等相关知识,匿名希望在学习及工作中可以帮助到您
HTML5版径向渐变梯度色彩

有个读者问我如何用HTML5生成一个径向梯度色彩效果,而不使用图片。仔细思考下,其实这个问题一点都不难,请看代码。

1124.jpg

HTML

<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8" />
<meta name="author" content="Script Tutorials" />
<title>HTML5 Radial Gradient | Script Tutorials</title>
<!-- add styles -->
<link href="css/main.css" rel="stylesheet" type="text/css" />
<!-- add script -->
<script src="js/script.js">
</script>
</head>
<body>
<div>
<canvas id="gradient" width="500" height="500" tabindex="1">
</canvas>
</div>
</body>
</html>

JS

// Get angle color function
function getAngleColor(angle) {
var color, d;

if (angle < Math.PI * 2 / 5) { // angle: 0-72
d = 255 / (Math.PI * 2 / 5) * angle;
color = '255,' + Math.round(d) + ',0'; // color: 255,0,0 - 255,255,0
} else if (angle < Math.PI * 4 / 5) { // angle: 72-144
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 2 / 5);
color = (255 - Math.round(d)) + ',255,0'; // color: 255,255,0 - 0,255,0
} else if (angle < Math.PI * 6 / 5) { // angle: 144-216
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 4 / 5);
color = '0,255,' + Math.round(d); // color: 0,255,0 - 0,255,255
} else if (angle < Math.PI * 8 / 5) { // angle: 216-288
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 6 / 5);
color = '0,'+(255 - Math.round(d)) + ',255'; // color: 0,255,255 - 0,0,255
} else { // angle: 288-360
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 8 / 5);
color = Math.round(d) + ',0,' + (255 - Math.round(d)) ; // color: 0,0,255 - 255,0,0
}
return color;
}

// inner variables
var iSectors = 360;
var iSectorAngle = (360 / iSectors) / 180 * Math.PI; // in radians

// Draw radial gradient function
function drawGradient() {

// prepare canvas and context objects
var canvas = document.getElementById('gradient');
var ctx = canvas.getContext('2d');

// clear canvas
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

// save current context
ctx.save();

// move to center
ctx.translate(canvas.width / 2, canvas.height / 2);

// draw all sectors
for (var i = 0; i < iSectors; i++) {

// start and end angles (in radians)
var startAngle = 0;
var endAngle = startAngle + iSectorAngle;

// radius for sectors
var radius = (canvas.width / 2) - 1;

// get angle color
var color = getAngleColor(iSectorAngle * i);

// draw a sector
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.arc(0, 0, radius, startAngle, endAngle, false);
ctx.closePath();

// stroke a sector
ctx.strokeStyle = 'rgb('+color+')';
ctx.stroke();

// fill a sector
ctx.fillStyle = 'rgb('+color+')';
ctx.fill();

// rotate to the next sector
ctx.rotate(iSectorAngle);
}

// restore context
ctx.restore();
}

// initialization
if(window.attachEvent) {
window.attachEvent('onload', drawGradient);
} else {
if(window.onload) {
var curronload = window.onload;
var newonload = function() {
curronload();
drawGradient();
};
window.onload = newonload;
} else {
window.onload = drawGradient;
}
}

以上就是HTML5版径向渐变梯度色彩的内容,更多相关内容请关注微课江湖()!

您可能想查找下面的文章:

  • HTML5知识点总结
  • HTML5的本地存储
  • HTML5本地存储之IndexedDB
  • Html5实现文件异步上传功能
  • Html5新标签datalist实现输入框与后台数据库数据的动态匹配
  • 详解HTML5 window.postMessage与跨域
  • HTML5拖放API实现拖放排序的实例代码
  • 解决html5中video标签无法播放mp4问题的办法
  • HTML5新特性 多线程(Worker SharedWorker)
  • Html5新增标签有哪些

相关文章

  • 2018-12-03目前的 HTML5 开发跟一年前比进展如何,国内国外有没有区别?
  • 2017-08-06HTML5的革新 结构之美
  • 2018-12-03HTML5实践-使用css制作时间ICON的具体详解(图)
  • 2018-12-03HTML5的canvas绘制动态时钟,1秒刷新一次canvas,个人觉得有性能的问题,如果不这么做的话,有什么好的方法?
  • 2018-12-03怎样用canvas实现自定义头像功能
  • 2018-12-03有没人用 ionic + AngularJS + PhoneGap开发过HybridApp?
  • 2018-12-03HTML5 Canvas实现文本对齐的代码总结
  • 2018-12-03 小强的HTML5移动开发之路(3)——HTML5与HTML4比较
  • 2017-08-06HTML5的标签的代码的简单介绍 HTML5标签的简介
  • 2018-12-03H5读取文件并上传到服务器的方法

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • HTML5中你不知道的5个新功能
    • HTML5应用-生日快乐动画之实现星星的示例代码分享
    • HTML5 data-* 自定义属性实例分享
    • HTML5 Canvas——用路径描画线条实例介绍_html5教程技巧
    • pushstate、popstate操作url的方法
    • HTML5手机端弹出遮罩菜单特效代码
    • HTML5 canvas实现瀑布流文字效果代码
    • 分享29个基于Bootstrap的HTML5响应式网页设计模板_html5教程技巧
    • HTML5的本地存储IndexedDB
    • 详细介绍HTML5实现3D迷宫的代码案例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有