📄 canvas.js
字号:
this.last.y = args[args.length - 1]; this.lastControl = {}; }, _moveToR: function(result, action, args){ if("x" in this.last){ result.push("moveTo", [this.last.x += args[0], this.last.y += args[1]]); }else{ result.push("moveTo", [this.last.x = args[0], this.last.y = args[1]]); } for(var i = 2; i < args.length; i += 2){ result.push("lineTo", [this.last.x += args[i], this.last.y += args[i + 1]]); } this.lastControl = {}; }, _lineToA: function(result, action, args){ for(var i = 0; i < args.length; i += 2){ result.push("lineTo", [args[i], args[i + 1]]); } this.last.x = args[args.length - 2]; this.last.y = args[args.length - 1]; this.lastControl = {}; }, _lineToR: function(result, action, args){ for(var i = 0; i < args.length; i += 2){ result.push("lineTo", [this.last.x += args[i], this.last.y += args[i + 1]]); } this.lastControl = {}; }, _hLineToA: function(result, action, args){ for(var i = 0; i < args.length; ++i){ result.push("lineTo", [args[i], this.last.y]); } this.last.x = args[args.length - 1]; this.lastControl = {}; }, _hLineToR: function(result, action, args){ for(var i = 0; i < args.length; ++i){ result.push("lineTo", [this.last.x += args[i], this.last.y]); } this.lastControl = {}; }, _vLineToA: function(result, action, args){ for(var i = 0; i < args.length; ++i){ result.push("lineTo", [this.last.x, args[i]]); } this.last.y = args[args.length - 1]; this.lastControl = {}; }, _vLineToR: function(result, action, args){ for(var i = 0; i < args.length; ++i){ result.push("lineTo", [this.last.x, this.last.y += args[i]]); } this.lastControl = {}; }, _curveToA: function(result, action, args){ for(var i = 0; i < args.length; i += 6){ result.push("bezierCurveTo", args.slice(i, i + 6)); } this.last.x = args[args.length - 2]; this.last.y = args[args.length - 1]; this.lastControl.x = args[args.length - 4]; this.lastControl.y = args[args.length - 3]; this.lastControl.type = "C"; }, _curveToR: function(result, action, args){ for(var i = 0; i < args.length; i += 6){ result.push("bezierCurveTo", [ this.last.x + args[i], this.last.y + args[i + 1], this.lastControl.x = this.last.x + args[i + 2], this.lastControl.y = this.last.y + args[i + 3], this.last.x + args[i + 4], this.last.y + args[i + 5] ]); this.last.x += args[i + 4]; this.last.y += args[i + 5]; } this.lastControl.type = "C"; }, _smoothCurveToA: function(result, action, args){ for(var i = 0; i < args.length; i += 4){ var valid = this.lastControl.type == "C"; result.push("bezierCurveTo", [ valid ? 2 * this.last.x - this.lastControl.x : this.last.x, valid ? 2 * this.last.y - this.lastControl.y : this.last.y, args[i], args[i + 1], args[i + 2], args[i + 3] ]); this.lastControl.x = args[i]; this.lastControl.y = args[i + 1]; this.lastControl.type = "C"; } this.last.x = args[args.length - 2]; this.last.y = args[args.length - 1]; }, _smoothCurveToR: function(result, action, args){ for(var i = 0; i < args.length; i += 4){ var valid = this.lastControl.type == "C"; result.push("bezierCurveTo", [ valid ? 2 * this.last.x - this.lastControl.x : this.last.x, valid ? 2 * this.last.y - this.lastControl.y : this.last.y, this.last.x + args[i], this.last.y + args[i + 1], this.last.x + args[i + 2], this.last.y + args[i + 3] ]); this.lastControl.x = this.last.x + args[i]; this.lastControl.y = this.last.y + args[i + 1]; this.lastControl.type = "C"; this.last.x += args[i + 2]; this.last.y += args[i + 3]; } }, _qCurveToA: function(result, action, args){ for(var i = 0; i < args.length; i += 4){ result.push("quadraticCurveTo", args.slice(i, i + 4)); } this.last.x = args[args.length - 2]; this.last.y = args[args.length - 1]; this.lastControl.x = args[args.length - 4]; this.lastControl.y = args[args.length - 3]; this.lastControl.type = "Q"; }, _qCurveToR: function(result, action, args){ for(var i = 0; i < args.length; i += 4){ result.push("quadraticCurveTo", [ this.lastControl.x = this.last.x + args[i], this.lastControl.y = this.last.y + args[i + 1], this.last.x + args[i + 2], this.last.y + args[i + 3] ]); this.last.x += args[i + 2]; this.last.y += args[i + 3]; } this.lastControl.type = "Q"; }, _qSmoothCurveToA: function(result, action, args){ for(var i = 0; i < args.length; i += 2){ var valid = this.lastControl.type == "Q"; result.push("quadraticCurveTo", [ this.lastControl.x = valid ? 2 * this.last.x - this.lastControl.x : this.last.x, this.lastControl.y = valid ? 2 * this.last.y - this.lastControl.y : this.last.y, args[i], args[i + 1] ]); this.lastControl.type = "Q"; } this.last.x = args[args.length - 2]; this.last.y = args[args.length - 1]; }, _qSmoothCurveToR: function(result, action, args){ for(var i = 0; i < args.length; i += 2){ var valid = this.lastControl.type == "Q"; result.push("quadraticCurveTo", [ this.lastControl.x = valid ? 2 * this.last.x - this.lastControl.x : this.last.x, this.lastControl.y = valid ? 2 * this.last.y - this.lastControl.y : this.last.y, this.last.x + args[i], this.last.y + args[i + 1] ]); this.lastControl.type = "Q"; this.last.x += args[i]; this.last.y += args[i + 1]; } }, _arcTo: function(result, action, args){ var relative = action == "a"; for(var i = 0; i < args.length; i += 7){ var x1 = args[i + 5], y1 = args[i + 6]; if(relative){ x1 += this.last.x; y1 += this.last.y; } var arcs = ga.arcAsBezier( this.last, args[i], args[i + 1], args[i + 2], args[i + 3] ? 1 : 0, args[i + 4] ? 1 : 0, x1, y1 ); dojo.forEach(arcs, function(p){ result.push("bezierCurveTo", p); }); this.last.x = x1; this.last.y = y1; } this.lastControl = {}; }, _closePath: function(result, action, args){ result.push("closePath", []); this.lastControl = {}; } }); dojo.forEach(["moveTo", "lineTo", "hLineTo", "vLineTo", "curveTo", "smoothCurveTo", "qCurveTo", "qSmoothCurveTo", "arcTo", "closePath"], function(method){ modifyMethod(g.Path, method); } ); dojo.declare("dojox.gfx.TextPath", g.path.TextPath, { // summary: a text shape (Canvas) _renderShape: function(/* Object */ ctx){ var s = this.shape; // nothing for the moment } }); dojo.declare("dojox.gfx.Surface", gs.Surface, { // summary: a surface object to be used for drawings (Canvas) constructor: function(){ gs.Container._init.call(this); this.pendingImageCount = 0; this.makeDirty(); }, setDimensions: function(width, height){ // summary: sets the width and height of the rawNode // width: String: width of surface, e.g., "100px" // height: String: height of surface, e.g., "100px" this.width = g.normalizedLength(width); // in pixels this.height = g.normalizedLength(height); // in pixels if(!this.rawNode) return this; this.rawNode.width = width; this.rawNode.height = height; this.makeDirty(); return this; // self }, getDimensions: function(){ // summary: returns an object with properties "width" and "height" return this.rawNode ? {width: this.rawNode.width, height: this.rawNode.height} : null; // Object }, _render: function(){ // summary: render the all shapes if(this.pendingImageCount){ return; } var ctx = this.rawNode.getContext("2d"); ctx.save(); ctx.clearRect(0, 0, this.rawNode.width, this.rawNode.height); for(var i = 0; i < this.children.length; ++i){ this.children[i]._render(ctx); } ctx.restore(); if("pendingRender" in this){ clearTimeout(this.pendingRender); delete this.pendingRender; } }, makeDirty: function(){ // summary: internal method, which is called when we may need to redraw if(!this.pendingImagesCount && !("pendingRender" in this)){ this.pendingRender = setTimeout(dojo.hitch(this, this._render), 0); } }, downloadImage: function(img, url){ // summary: // internal method, which starts an image download and renders, when it is ready // img: Image: // the image object // url: String: // the url of the image var handler = dojo.hitch(this, this.onImageLoad); if(!this.pendingImageCount++ && "pendingRender" in this){ clearTimeout(this.pendingRender); delete this.pendingRender; } img.onload = handler; img.onerror = handler; img.onabort = handler; img.src = url; }, onImageLoad: function(){ if(!--this.pendingImageCount){ this._render(); } }, // events are not implemented getEventSource: function(){ return null; }, connect: function(){}, disconnect: function(){} }); g.createSurface = function(parentNode, width, height){ // summary: creates a surface (Canvas) // parentNode: Node: a parent node // width: String: width of surface, e.g., "100px" // height: String: height of surface, e.g., "100px" if(!width){ width = "100%"; } if(!height){ height = "100%"; } var s = new g.Surface(), p = dojo.byId(parentNode), c = p.ownerDocument.createElement("canvas"); c.width = width; c.height = height; p.appendChild(c); s.rawNode = c; s.surface = s; return s; // dojox.gfx.Surface }; // Extenders var C = gs.Container, Container = { add: function(shape){ this.surface.makeDirty(); return C.add.apply(this, arguments); }, remove: function(shape, silently){ this.surface.makeDirty(); return C.remove.apply(this, arguments); }, clear: function(){ this.surface.makeDirty(); return C.clear.apply(this, arguments); }, _moveChildToFront: function(shape){ this.surface.makeDirty(); return C._moveChildToFront.apply(this, arguments); }, _moveChildToBack: function(shape){ this.surface.makeDirty(); return C._moveChildToBack.apply(this, arguments); } }; dojo.mixin(gs.Creator, { // summary: Canvas shape creators createObject: function(shapeType, rawShape) { // summary: creates an instance of the passed shapeType class // shapeType: Function: a class constructor to create an instance of // rawShape: Object: properties to be passed in to the classes "setShape" method // overrideSize: Boolean: set the size explicitly, if true var shape = new shapeType(); shape.surface = this.surface; shape.setShape(rawShape); this.add(shape); return shape; // dojox.gfx.Shape } }); dojo.extend(g.Group, Container); dojo.extend(g.Group, gs.Creator); dojo.extend(g.Surface, Container); dojo.extend(g.Surface, gs.Creator);})();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -