首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
canvas 笔记之圆
时间:
2014-09-28 00:31:01
阅读:
168
评论:
0
收藏:
0
[点我收藏+]
标签:
style
color
io
os
ar
for
sp
art
on
/**
* 圆形进度条
* 描述:1、支持在环形进度条中显示百分比
* 2、支持绕环渐变
*/
function drawProcess()
{
var canvas = $("canvas")[0];
var context = canvas.getContext(‘2d‘);
context.clearRect(0, 0, 100, 100);
context.fillStyle = "transparent"
canvas.width = 100;
canvas.height = 100;
// 画灰色的圆
var drawGrayCircle = function() {
context.beginPath();
context.arc(50, 50, 50, 0, Math.PI * 2, false);
context.closePath();
context.fillStyle = ‘#ddd‘;
context.fill();
};
var process = 0; // 进度
var linear = context.createLinearGradient(0, 0, 100, 100);
var drawRed = function() {
context.save();
// 画红色的圆
context.beginPath();
var startDeg = -Math.PI / 2 + Math.PI * 2 * process / 100;
var endDeg = -Math.PI / 2 + Math.PI * 2 * (++process) / 100;
context.moveTo(50, 50);
context.arc(50, 50, 50, startDeg, endDeg, false);
// context.translate(50, 50);
// context.rotate(-Math.PI / 2);
context.fillStyle = context.strokeStyle = getCurColor(process);
context.closePath();
context.fill();
context.stroke();
context.restore();
drawEmptyCircle();
drawText();
progress = setTimeout(drawRed, 1000 / 60);
if (process == 100)
{
clearTimeout(progress);
progress = null;
console.log(canvas.toDataURL("image/jpeg"))
}
if (process == 60)
{
console.log(canvas.toDataURL("image/png"));
console.log(context.lineWidth);
}
};
var progress = setTimeout(drawRed, 1000 / 60);
/**
* 画空的圆
*/
var drawEmptyCircle = function() {
context.beginPath();
context.arc(50, 50, 45, 0, Math.PI * 2, true);
context.closePath();
context.fillStyle = ‘rgba(255,255,255,1)‘;
context.fill();
};
// 写字
var drawText = function() {
context.save();
context.font = "bold 14px Arial";
context.fillStyle = ‘green‘;
context.textAlign = ‘center‘;
context.textBaseline = ‘middle‘;
// context.translate(50, 50);
// context.rotate(-Math.PI / 2);
context.fillText(process + "%", 50, 50);
context.restore();
};
/**
* 画轮廓
*/
var drawOutline = function(){
// 保存上下文状态
context.save();
context.beginPath();
context.arc(50, 50, 46, 0, Math.PI * 2, true);
context.closePath();
context.lineWidth = 1;
context.strokeStyle = ‘yellow‘;
context.stroke();
// 恢复上下文
context.restore();
};
var startColor = ‘rgb(249, 72, 80)‘;
var endColor = ‘rgb(255, 255, 255)‘;
var rStep = (255 - 249) / 100;
var gStep = (255 - 72) / 100;
var bStep = (255 - 80) / 100;
var getCurColor = function(deg){
var normalDeg = deg * 180 / Math.PI;
var aRgb = [];
aRgb.push((249 + rStep * process).toFixed(0));
aRgb.push((72 + gStep * process).toFixed(0));
aRgb.push((80 + bStep * process).toFixed(0));
return ‘rgb(‘ + aRgb.join(‘,‘) + ‘)‘;
};
drawOutline();
setTimeout(function(){
$(‘<iframe id="frame"></iframe>‘).appendTo("body")
canvas.onclick = function(){
// var o = window.open(canvas.toDataURL(), "","width=1, height=1,top=5000,left=5000");
//// location.href = canvas.toDataURL();
// o.document.execCommand("saveas")
// o.close()
document.all("frame").location = canvas.toDataURL()
document.execCommand("SaveAs");
}
})
}
drawProcess();
/**
* 饼图1
* 描述:1、支持动画效果
* 2、支持在扇形上显示百分比
* 3、支持动画
* 4、扇形的个数和颜色很容易配置
*/
function drawCircle()
{
var PI = 3.14;
// var PI = Math.PI;
var colors = ["#4F81BD", "#C0504D", "#9BBB59", "red", "#3c3c3c", "green"];
var data = [30, 20, 10, 20, 10, 10];
var canvas = document.getElementsByTagName("canvas")[0];
var ctx = canvas.getContext("2d");
var startDeg = 0;
var requestFrame = {};
var per = PI * 2 / 100;
for(var i = 0, length = data.length; i < length; i++)
{
requestFrame[‘percentage‘ + i] = data[i] + "%";
}
var doDraw = function(startDeg, endDeg, color, index){
if(startDeg < endDeg)
{
ctx.save();
var curDeg = startDeg + per;
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.arc(50, 50, 50, startDeg, curDeg, false);
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.restore();
requestFrame[i] = requestAnimationFrame(doDraw.bind(this, curDeg, endDeg, color, index));
}
else
{
cancelAnimationFrame(requestFrame[index]);
// ctx.clearRect(0, 0, 100, 100);
ctx.save();
ctx.fillStyle = "red";
ctx.beginPath();
ctx.translate(50, 50);
ctx.arc(0, 0, 5, 0, PI * 2, false);
ctx.closePath();
ctx.fill();
console.log("%c" + requestFrame[‘startDeg‘ + index] + "~~~~~~~~" + requestFrame[‘endDeg‘ + index], "font-size:14px;color:purple;")
var rotateDeg = (requestFrame[‘endDeg‘ + index] - requestFrame[‘startDeg‘ + index]) / 2 + requestFrame[‘startDeg‘ + index];
ctx.beginPath();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "14px";
ctx.fillStyle = "#FFFFFF";
if(rotateDeg <= 3 / 2 * PI && rotateDeg > 1 / 2 * PI)
{
rotateDeg += PI;
ctx.rotate(rotateDeg);
ctx.translate(-50, 0);
}
else
{
ctx.rotate(rotateDeg);
}
ctx.fillText(requestFrame[‘percentage‘ + index], 25, 0);
ctx.closePath();
ctx.restore();
}
};
canvas.width = 100;
canvas.height = 100;
for (var i = 0; i < data.length; i++)
{
var endDeg = startDeg + per * data[i];
requestFrame[‘startDeg‘ + i] = startDeg;
requestFrame[‘endDeg‘ + i] = endDeg;
requestFrame[i] = requestAnimationFrame(doDraw.bind(this, startDeg, endDeg, colors[i], i));
console.log(startDeg + "~~~~" + endDeg)
startDeg = endDeg;
}
}
drawCircle();
/**
* 饼图2 无特效
*/
var color = ["#27255F","#2F368F","#3666B0"];
var data = [50, 20, 30];
function drawCircle(){
var canvas = document.getElementsByTagName("canvas")[0];
var ctx = canvas.getContext("2d");
var startPoint=0;
canvas.height = 155
for(var i=0;i<data.length;i++){
ctx.fillStyle = color[i];
ctx.beginPath();
ctx.moveTo(100,100);
ctx.strokeStyle = color[i];
var endDeg = startPoint+Math.PI*2*(data[i]/100)
ctx.arc(100,100,50,startPoint,endDeg,false);
console.log(startPoint+"~~~~"+endDeg)
ctx.fill();
ctx.stroke()
startPoint+=Math.PI*2*(data[i]/100);
}
}
drawCircle();
canvas 笔记之圆
标签:
style
color
io
os
ar
for
sp
art
on
原文地址:http://my.oschina.net/u/1259707/blog/322715
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!