Html
<! DOCTYPE html>
<title>canvas Doraemon </title>
<body>
<canvas id= "Canvas" width= "800px" height= "1000px" > your browser does not support canvas, Please upgrade or replace other browsers !</canvas >
</body>
?
?
Css
HTML {
Background: #fff;
}
?
?
Js
function Mlog (log) {
Console.log ("Info:" +log);
};
function Zuobiao (ctx, width, height) {
Ctx.fillstyle = "BLACK";
for (var j = 0; j <= height; j+=50) {
Ctx.linewidth = 1;
Ctx.linestyle = "Red";
Ctx.beginpath ();
Ctx.moveto (0,J);
Ctx.lineto (WIDTH,J);
Ctx.stroke ();
}
for (var i = 0; I <= width; i+=50) {
Ctx.linewidth = 1;
Ctx.linestyle = "#FF0000";
Ctx.beginpath ();
Ctx.moveto (i,0);
Ctx.lineto (I,height);
Ctx.stroke ();
}
for (var i = 0; I <= width; i+=50) {
for (var j = 0; j <= height; j+=50) {
ctx.font= "12px Arill";
Ctx.beginpath ();
Ctx.filltext (i+ "," +j, I, J);
Ctx.stroke ();
}
}
}
(function () {
Mlog ("Hi,i ' m vanliga!");
var ctx = init ();
Left
Ctx.linewidth = 9;
Ctx.linestyle= "BLACK";
Ctx.fillstyle = "#FFFFFF";
Ctx.beginpath ();
Ctx.arc (654, 543, 0, 2*math.pi, true);
Ctx.stroke ();
Ctx.fill ();
Two feet
BezierEllipse2 (CTX, 720, 98, 5, "Black", "");
BezierEllipse2 (CTX, 530, 720, 98, 5, "Black", "");
Body
Ctx.linewidth = 9;
Ctx.linestyle= "BLACK";
Ctx.linecap = "Around";
Ctx.fillstyle = "#00A0E9";
Ctx.beginpath ();
Ctx.moveto (568,445);
Ctx.quadraticcurveto (630,478,650,500);
Ctx.quadraticcurveto (650,559,600,550);
Ctx.quadraticcurveto (579,540,550,518);
Ctx.quadraticcurveto (553,510,550,480);
Ctx.quadraticcurveto (553,510,550,518);
Ctx.quadraticcurveto (557,510,548,650);
Ctx.quadraticcurveto (579,680,570,700);
Ctx.quadraticcurveto (500,720,440,700);
Ctx.quadraticcurveto (400,650,360,700);
Ctx.quadraticcurveto (300,720,230,700);
Ctx.quadraticcurveto (224,675,250,650);
Ctx.quadraticcurveto (253,510,259,480);
Ctx.quadraticcurveto (253,510,255,520);
Ctx.quadraticcurveto (253,510,100,400);
Ctx.quadraticcurveto (120,360,150,350);
Ctx.quadraticcurveto (203,380,220,375);
Ctx.quadraticcurveto (203,380,568,445);
Ctx.stroke ();
Ctx.fill ();
BezierEllipse2 (CTX, 5,---------"#FFFFFF", "fill");
BezierEllipse2 (CTX, +, 5, "Black", "" ");
Ctx.linewidth = 4;
Ctx.beginpath ();
Ctx.moveto (325,600);
Ctx.quadraticcurveto (400,610,475,600);
Ctx.quadraticcurveto (400,700,325,600);
Ctx.stroke ();
Face
Ctx.linewidth = 5;
Ctx.linestyle= "BLACK";
Ctx.fillstyle = "#00A0E9";
Ctx.beginpath ();
Ctx.arc (0, 2*math.pi, true);
Ctx.fill ();
Ctx.stroke ();
Ctx.linewidth = 5;
Ctx.linestyle= "BLACK";
Ctx.fillstyle = "#FFFFFF";
Ctx.beginpath ();
CTX.ARC (340, 0, 2*math.pi, true);
Ctx.fill ();
Ctx.stroke ();
Eyes
BezierEllipse2 (CTX, 345, 5, "#FFFFFF", "fill");
BezierEllipse2 (CTX, 345, 5, "Black", "" ");
BezierEllipse2 (CTX, 455, 5, "#FFFFFF", "fill");
BezierEllipse2 (CTX, 455, 5, "Black", "" ");
BezierEllipse2 (CTX, 365, 5, "#000", "fill");
BezierEllipse2 (CTX, 435, 5, "#000", "fill");
BezierEllipse2 (CTX, 375, 8, 5, "#FFFFFF", "fill");
BezierEllipse2 (CTX, 425, 8, 5, "#FFFFFF", "fill");
Nose
Ctx.linewidth = 5;
Ctx.linestyle= "BLACK";
Ctx.fillstyle = "#E60012";
Ctx.beginpath ();
Ctx.arc (280, 0, 2*math.pi, true);
Ctx.fill ();
Ctx.stroke ();
Ctx.fillstyle = "#FFFFFF";
Ctx.beginpath ();
Ctx.arc (390, +, 0, 2*math.pi, true);
Ctx.fill ();
Ctx.beginpath ();
Ctx.moveto (400, 320);
Ctx.lineto (400, 460);
Ctx.stroke ();
Mouth
Ctx.linewidth = 5;
Ctx.linestyle= "BLACK";
Ctx.linecap = "Round";
Ctx.beginpath ();
Ctx.moveto (280,410);
Ctx.beziercurveto (360,480,440,480,520,410);
Ctx.stroke ();
Left Beard
Ctx.moveto (360,320);
Ctx.lineto (270,260);
Ctx.stroke ();
Ctx.moveto (360,350);
Ctx.lineto (250,340);
Ctx.stroke ();
Ctx.moveto (360,380);
Ctx.lineto (260,400);
Ctx.stroke ();
Right Beard
Ctx.moveto (440,320);
Ctx.lineto (530,260);
Ctx.stroke ();
Ctx.moveto (440,350);
Ctx.lineto (550,340);
Ctx.stroke ();
Ctx.moveto (440,380);
Ctx.lineto (540,400);
Ctx.stroke ();
Scarf
Ctx.linewidth = 7;
Ctx.linestyle= "BLACK";
Ctx.fillstyle = "#E60012";
Ctx.beginpath ();
Ctx.moveto (240,427);
Ctx.quadraticcurveto (400,530,560,427);
Ctx.quadraticcurveto (570,437,560,447);
Ctx.quadraticcurveto (400,560,240,450);
Ctx.quadraticcurveto (230,437,242,427);
Ctx.stroke ();
Ctx.fill ();
Bell
Ctx.linewidth = 9;
Ctx.linestyle= "BLACK";
Ctx.fillstyle = "#FDD000";
Ctx.beginpath ();
Ctx.arc (530, 0, 2*math.pi, true);
Ctx.stroke ();
Ctx.fill ();
Ctx.linewidth = 8;
Ctx.linestyle= "BLACK";
Ctx.linecap = "Around";
Ctx.fillstyle = "#FDD000";
Ctx.beginpath ();
Ctx.moveto (365,510);
Ctx.quadraticcurveto (400,498,435,510);
Ctx.quadraticcurveto (450,510,445,518);
Ctx.quadraticcurveto (400,505,355,518);
Ctx.quadraticcurveto (350,510,365,510);
Ctx.stroke ();
Ctx.fill ();
Ctx.linewidth = 8;
Ctx.linestyle= "BLACK";
Ctx.fillstyle = "#7D6B56";
Ctx.beginpath ();
Ctx.arc (542, 8, 0, 2*math.pi, true);
Ctx.stroke ();
Ctx.fill ();
Ctx.linewidth = 5;
Ctx.linestyle= "BLACK";
Ctx.beginpath ();
Ctx.moveto (400, 554);
Ctx.lineto (400, 570);
Ctx.stroke ();
Right
Ctx.linewidth = 9;
Ctx.linestyle= "BLACK";
Ctx.fillstyle = "#FFFFFF";
Ctx.beginpath ();
Ctx.arc (+, 0, 2*math.pi, true);
Ctx.stroke ();
Ctx.fill ();
Zuobiao (CTX, 800, 1000);
})();
Function init () {
var can = document.getElementById ("canvas");
//can.style.border = "1px solid red";
VA R CTX = Can.getcontext ("2d");
Return CTX;
}
How to draw an ellipse
/*context is a 2D drawing environment object for canvas,
X is the center axis of the ellipse,
Y is the center ordinate of the ellipse,
A is an elliptical transverse half shaft length,
B is oval longitudinal half axis length */
function BezierEllipse2 (CTX, x, Y, a, B, linewidth, color, method)
{
var k =. 5522848,
Ox = A * k,//horizontal Control point offset
Oy = b * k; Vertical Control Point Offset
if (method = = "Fill") {
Ctx.fillstyle = color;
}
else{
Ctx.linewidth = linewidth;
Ctx.storkestyle = color;
}
Ctx.beginpath ();
Draw four three Bezier curves clockwise from the left end of the ellipse
Ctx.moveto (x-a, y);
Ctx.beziercurveto (X-a, Y-oy, X-ox, Y-b, X, Y-b);
Ctx.beziercurveto (x + ox, y-b, X + A, Y-oy, X + A, y);
Ctx.beziercurveto (x + A, y + Oy, x + ox, y + b, x, y + b);
Ctx.beziercurveto (X-ox, y + b, x-a, Y + Oy, x-a, y);
Ctx.closepath ();
if (method = = "Fill") {
Ctx.fill ();
}
else{
Ctx.stroke ();
}
};
?