canvas 笔记之圆

/**

* 圆形进度条

* 描述: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();

时间: 2024-10-11 20:52:02

canvas 笔记之圆的相关文章

canvas之画圆

<canvas id="canvas" width="500" height="500" style="background-color: yellow;"></canvas> 1 var canvas=document.getElementById("canvas"); 2 var cxt=canvas.getContext("2d"); 3 //画一个空心圆

html5 canvas 笔记二(添加样式和颜色)

色彩 Colors fillStyle = color 设置图形的填充颜色. strokeStyle = color 设置图形轮廓的颜色. 透明度 Transparency globalAlpha = transparency value 1 // globalAlpha 示例 2 ctx.fillStyle = '#FD0'; 3 ctx.globalAlpha = 0.2; 4 5 // rgba() 示例 6 ctx.strokeStyle = "rgba(255,0,0,0.5)&quo

canvas笔记

1. 清除矩形区域 context.clearRect(x,y,width,height)      x:清除矩形起点横坐标      y:清除矩形起点纵坐标      width:清除矩形长度      height:清除矩形高度 2. 绘制矩形  context.fillRect(x,y,width,height)  strokeRect(x,y,width,height)      x:矩形起点横坐标      y:矩形起点纵坐标      width:矩形长度      height:矩

html5 canvas 笔记五(合成与裁剪)

组合 Compositing globalCompositeOperation syntax: globalCompositeOperation = type 注意:下面所有例子中,蓝色方块是先绘制的,即“已有的 canvas 内容”,红色圆形是后面绘制,即“新图形”. source-over 这是默认设置,新图形会覆盖在原有内容之上. destination-over 会在原有内容之下绘制新图形. source-in 新图形会仅仅出现与原有内容重叠的部分.其它区域都变成透明的. destina

Html5 Canvas笔记(3)-Canvas状态

p{ font-size: 15px; text-indent: 2em; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; padding: 5px; margin: 1em 0 1em 0; } .alexrootdiv>div>p:first-of-type,.alextitlep{ font-size: 18px; font-weight: bold; color: red;

Html5 Canvas笔记(2)-Canvas绘图

用Canvas API绘图,需要画图形的边线并设置内部区域填充,边线英文语法对应stroke,填充对应fill,在后面我们会频繁看到这2个英文单词的出现.Canvas API内置的形状绘图函数比较少,可以完成绘制矩形Rect.弧形Arc,也可以画贝塞尔曲线bezierCurver.quadraticCurve.下面我们一个一个的来说. 画线-Line 1 function drawScreen(){ 2 context.strokeStyle='black'; 3 context.lineWid

Canvas 笔记(持续更新中)

1.从线条开始 HTML <canvas id="canvas"></canvas> Javascript var canvas=document.getElementById("canvas"); var context=canvas.getContext("2d") canvas.width canvas.height canvas.getContext("3d") moveTo(x,y) line

安卓笔记:Canvas绘图

1.Canvas常用方法: drawRect(RectF rect, Paint paint) //绘制区域,参数一为RectF一个区域 drawPath(Path path, Paint paint) //绘制一个路径,参数一为Path路径对象 drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)  //贴图,参数一就是我们常规的Bitmap对象,参数二是源区域(这里是bitmap),参数三是目标区域(应该在canvas的位置和大

Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)

1.首先说一下canvas类: Class OverviewThe Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path,