📄 canvas.js
字号:
if(!dojo._hasResource["dojox.gfx.canvas"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.dojo._hasResource["dojox.gfx.canvas"] = true;dojo.provide("dojox.gfx.canvas");dojo.require("dojox.gfx._base");dojo.require("dojox.gfx.shape");dojo.require("dojox.gfx.path");dojo.require("dojox.gfx.arc");dojo.require("dojox.gfx.decompose");dojo.experimental("dojox.gfx.canvas");(function(){ var g = dojox.gfx, gs = g.shape, ga = g.arc, m = g.matrix, mp = m.multiplyPoint, pi = Math.PI, twoPI = 2 * pi, halfPI = pi /2; dojo.extend(g.Shape, { _render: function(/* Object */ ctx){ // summary: render the shape ctx.save(); this._renderTransform(ctx); this._renderShape(ctx); this._renderFill(ctx, true); this._renderStroke(ctx, true); ctx.restore(); }, _renderTransform: function(/* Object */ ctx){ if("canvasTransform" in this){ var t = this.canvasTransform; ctx.translate(t.dx, t.dy); ctx.rotate(t.angle2); ctx.scale(t.sx, t.sy); ctx.rotate(t.angle1); // The future implementation when vendors catch up with the spec: // var t = this.matrix; // ctx.transform(t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); } }, _renderShape: function(/* Object */ ctx){ // nothing }, _renderFill: function(/* Object */ ctx, /* Boolean */ apply){ if("canvasFill" in this){ if("canvasFillImage" in this){ this.canvasFill = ctx.createPattern(this.canvasFillImage, "repeat"); delete this.canvasFillImage; } ctx.fillStyle = this.canvasFill; if(apply){ ctx.fill(); } }else{ ctx.fillStyle = "rgba(0,0,0,0.0)"; } }, _renderStroke: function(/* Object */ ctx, /* Boolean */ apply){ var s = this.strokeStyle; if(s){ ctx.strokeStyle = s.color.toString(); ctx.lineWidth = s.width; ctx.lineCap = s.cap; if(typeof s.join == "number"){ ctx.lineJoin = "miter"; ctx.miterLimit = s.join; }else{ ctx.lineJoin = s.join; } if(apply){ ctx.stroke(); } }else if(!apply){ ctx.strokeStyle = "rgba(0,0,0,0.0)"; } }, // events are not implemented getEventSource: function(){ return null; }, connect: function(){}, disconnect: function(){} }); var modifyMethod = function(shape, method, extra){ var old = shape.prototype[method]; shape.prototype[method] = extra ? function(){ this.surface.makeDirty(); old.apply(this, arguments); extra.call(this); return this; } : function(){ this.surface.makeDirty(); return old.apply(this, arguments); }; }; modifyMethod(g.Shape, "setTransform", function(){ // prepare Canvas-specific structures if(this.matrix){ this.canvasTransform = g.decompose(this.matrix); }else{ delete this.canvasTransform; } }); modifyMethod(g.Shape, "setFill", function(){ // prepare Canvas-specific structures var fs = this.fillStyle, f; if(fs){ if(typeof(fs) == "object" && "type" in fs){ var ctx = this.surface.rawNode.getContext("2d"); switch(fs.type){ case "linear": case "radial": f = fs.type == "linear" ? ctx.createLinearGradient(fs.x1, fs.y1, fs.x2, fs.y2) : ctx.createRadialGradient(fs.cx, fs.cy, 0, fs.cx, fs.cy, fs.r); dojo.forEach(fs.colors, function(step){ f.addColorStop(step.offset, g.normalizeColor(step.color).toString()); }); break; case "pattern": var img = new Image(fs.width, fs.height); this.surface.downloadImage(img, fs.src); this.canvasFillImage = img; } }else{ // Set fill color using CSS RGBA func style f = fs.toString(); } this.canvasFill = f; }else{ delete this.canvasFill; } }); modifyMethod(g.Shape, "setStroke"); modifyMethod(g.Shape, "setShape"); dojo.declare("dojox.gfx.Group", g.Shape, { // summary: a group shape (Canvas), which can be used // to logically group shapes (e.g, to propagate matricies) constructor: function(){ gs.Container._init.call(this); }, _render: function(/* Object */ ctx){ // summary: render the group ctx.save(); this._renderTransform(ctx); this._renderFill(ctx); this._renderStroke(ctx); for(var i = 0; i < this.children.length; ++i){ this.children[i]._render(ctx); } ctx.restore(); } }); dojo.declare("dojox.gfx.Rect", gs.Rect, { // summary: a rectangle shape (Canvas) _renderShape: function(/* Object */ ctx){ var s = this.shape, r = Math.min(s.r, s.height / 2, s.width / 2), xl = s.x, xr = xl + s.width, yt = s.y, yb = yt + s.height, xl2 = xl + r, xr2 = xr - r, yt2 = yt + r, yb2 = yb - r; ctx.beginPath(); ctx.moveTo(xl2, yt); if(r){ ctx.arc(xr2, yt2, r, -halfPI, 0, false); ctx.arc(xr2, yb2, r, 0, halfPI, false); ctx.arc(xl2, yb2, r, halfPI, pi, false); ctx.arc(xl2, yt2, r, pi, halfPI, false); }else{ ctx.lineTo(xr2, yt); ctx.lineTo(xr, yb2); ctx.lineTo(xl2, yb); ctx.lineTo(xl, yt2); } ctx.closePath(); } }); var bezierCircle = []; (function(){ var u = ga.curvePI4; bezierCircle.push(u.s, u.c1, u.c2, u.e); for(var a = 45; a < 360; a += 45){ var r = m.rotateg(a); bezierCircle.push(mp(r, u.c1), mp(r, u.c2), mp(r, u.e)); } })(); dojo.declare("dojox.gfx.Ellipse", gs.Ellipse, { // summary: an ellipse shape (Canvas) setShape: function(){ g.Ellipse.superclass.setShape.apply(this, arguments); // prepare Canvas-specific structures var s = this.shape, t, c1, c2, r = [], M = m.normalize([m.translate(s.cx, s.cy), m.scale(s.rx, s.ry)]); t = mp(M, bezierCircle[0]); r.push([t.x, t.y]); for(var i = 1; i < bezierCircle.length; i += 3){ c1 = mp(M, bezierCircle[i]); c2 = mp(M, bezierCircle[i + 1]); t = mp(M, bezierCircle[i + 2]); r.push([c1.x, c1.y, c2.x, c2.y, t.x, t.y]); } this.canvasEllipse = r; return this; }, _renderShape: function(/* Object */ ctx){ var r = this.canvasEllipse; ctx.beginPath(); ctx.moveTo.apply(ctx, r[0]); for(var i = 1; i < r.length; ++i){ ctx.bezierCurveTo.apply(ctx, r[i]); } ctx.closePath(); } }); dojo.declare("dojox.gfx.Circle", gs.Circle, { // summary: a circle shape (Canvas) _renderShape: function(/* Object */ ctx){ var s = this.shape; ctx.beginPath(); ctx.arc(s.cx, s.cy, s.r, 0, twoPI, 1); } }); dojo.declare("dojox.gfx.Line", gs.Line, { // summary: a line shape (Canvas) _renderShape: function(/* Object */ ctx){ var s = this.shape; ctx.beginPath(); ctx.moveTo(s.x1, s.y1); ctx.lineTo(s.x2, s.y2); } }); dojo.declare("dojox.gfx.Polyline", gs.Polyline, { // summary: a polyline/polygon shape (Canvas) setShape: function(){ g.Polyline.superclass.setShape.apply(this, arguments); // dojo.inherited("setShape", arguments); // prepare Canvas-specific structures var p = this.shape.points, f = p[0], r = [], c, i; if(p.length){ if(typeof f == "number"){ r.push(f, p[1]); i = 2; }else{ r.push(f.x, f.y); i = 1; } for(; i < p.length; ++i){ c = p[i]; if(typeof c == "number"){ r.push(c, p[++i]); }else{ r.push(c.x, c.y); } } } this.canvasPolyline = r; return this; }, _renderShape: function(/* Object */ ctx){ // console.debug("Polyline::_renderShape"); var p = this.canvasPolyline; if(p.length){ ctx.beginPath(); ctx.moveTo(p[0], p[1]); for(var i = 2; i < p.length; i += 2){ ctx.lineTo(p[i], p[i + 1]); } } } }); dojo.declare("dojox.gfx.Image", gs.Image, { // summary: an image shape (Canvas) setShape: function(){ g.Image.superclass.setShape.apply(this, arguments); // prepare Canvas-specific structures var img = new Image(); this.surface.downloadImage(img, this.shape.src); this.canvasImage = img; return this; }, _renderShape: function(/* Object */ ctx){ var s = this.shape; ctx.drawImage(this.canvasImage, s.x, s.y, s.width, s.height); } }); dojo.declare("dojox.gfx.Text", gs.Text, { // summary: a text shape (Canvas) _renderShape: function(/* Object */ ctx){ var s = this.shape; // nothing for the moment } }); modifyMethod(g.Text, "setFont"); var pathRenderers = { M: "_moveToA", m: "_moveToR", L: "_lineToA", l: "_lineToR", H: "_hLineToA", h: "_hLineToR", V: "_vLineToA", v: "_vLineToR", C: "_curveToA", c: "_curveToR", S: "_smoothCurveToA", s: "_smoothCurveToR", Q: "_qCurveToA", q: "_qCurveToR", T: "_qSmoothCurveToA", t: "_qSmoothCurveToR", A: "_arcTo", a: "_arcTo", Z: "_closePath", z: "_closePath" }; dojo.declare("dojox.gfx.Path", g.path.Path, { // summary: a path shape (Canvas) constructor: function(){ this.last = {}; this.lastControl = {}; }, setShape: function(){ this.canvasPath = []; return g.Path.superclass.setShape.apply(this, arguments); }, _updateWithSegment: function(segment){ var last = dojo.clone(this.last); this[pathRenderers[segment.action]](this.canvasPath, segment.action, segment.args); this.last = last; g.Path.superclass._updateWithSegment.apply(this, arguments); }, _renderShape: function(/* Object */ ctx){ var r = this.canvasPath; ctx.beginPath(); for(var i = 0; i < r.length; i += 2){ ctx[r[i]].apply(ctx, r[i + 1]); } }, _moveToA: function(result, action, args){ result.push("moveTo", [args[0], args[1]]); for(var i = 2; i < args.length; i += 2){ result.push("lineTo", [args[i], args[i + 1]]); } this.last.x = args[args.length - 2];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -