From eeb4cc32569c66d857745917eb2bce344f352111 Mon Sep 17 00:00:00 2001 From: simaQ Date: Thu, 8 Jun 2017 10:17:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=E5=91=BD=E5=90=8D=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- atool-test.config.js | 8 --- index.js | 4 +- package.json | 8 +-- src/canvas.js | 50 ++----------- src/event/event.js | 42 ----------- src/g/core/element.js | 5 +- src/g/core/group.js | 1 + .../core/mixin/event-dispatcher.js} | 10 +-- src/g/core/shape.js | 4 +- src/g/format.js | 5 +- src/g/shape/cubic.js | 16 ++--- src/g/shape/fan.js | 3 +- src/g/shape/image.js | 16 ++--- src/g/shape/math/arc.js | 7 +- src/g/shape/math/cubic.js | 9 ++- src/g/shape/math/quadratic.js | 4 +- src/g/shape/path.js | 4 +- src/g/shape/quadratic.js | 12 ++-- src/g/shape/text.js | 4 +- src/g/shape/util/pathSegment.js | 3 +- src/{event => }/mouse-event.js | 72 +++++++++---------- src/util/{common-util.js => common.js} | 44 ++++++++++-- src/util/{dom-util.js => dom.js} | 0 src/util/index.js | 4 +- src/util/{matrix-util.js => matrix.js} | 0 tests/canvas/canvas-spec.js | 3 - tests/g/core-eventDispatcher-spec.js | 3 +- tests/matrixUtil/matrixUtil-spec.js | 2 +- webpack.config.js | 18 +++++ 29 files changed, 153 insertions(+), 208 deletions(-) delete mode 100644 atool-test.config.js delete mode 100644 src/event/event.js rename src/{event/eventDispatcher.js => g/core/mixin/event-dispatcher.js} (92%) rename src/{event => }/mouse-event.js (74%) rename src/util/{common-util.js => common.js} (65%) rename src/util/{dom-util.js => dom.js} (100%) rename src/util/{matrix-util.js => matrix.js} (100%) create mode 100644 webpack.config.js diff --git a/atool-test.config.js b/atool-test.config.js deleted file mode 100644 index dfb9cce26..000000000 --- a/atool-test.config.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = (webpackConfig) => { - Object.assign(webpackConfig.output, { - library: 'G', - libraryTarget: 'var' - // umdNamedDefine: true, - }) - return webpackConfig -} diff --git a/index.js b/index.js index a52459d9d..12b004beb 100644 --- a/index.js +++ b/index.js @@ -13,8 +13,8 @@ Canvas.PathUtil = { pathToAbsolute: toAbsolute, // Util.path2Absolute catmullRom2bezier: catmullRomToBezier, }; -Canvas.MatrixUtil = require('./src/util/matrix-util'); -Canvas.DomUtil = require('./src/util/dom-util'); +Canvas.MatrixUtil = require('./src/util/matrix'); +Canvas.DomUtil = require('./src/util/dom'); Canvas.Matrix = require('@ali/g-matrix'); module.exports = Canvas; diff --git a/package.json b/package.json index 83ff50e90..d63c4e600 100644 --- a/package.json +++ b/package.json @@ -51,13 +51,13 @@ "index": "./index" }, "dependencies": { - "@ali/g-interpolation": "~0.0.0", - "@ali/g-matrix": "~0.0.0", - "@ali/g-math": "~1.0.0", "@ali/g-color": "~0.0.7", "@ali/g-event": "~0.0.0", + "@ali/g-interpolation": "~0.0.0", + "@ali/g-matrix": "~0.0.0", + "@ali/g-path-util": "~1.0.0", "@ali/g-tween": "~0.3.1-beta.1", - "@ali/g-path-util": "~1.0.0" + "gl-matrix": "^2.3.2" }, "pre-commit": { "run": [ diff --git a/src/canvas.js b/src/canvas.js index f73d36e38..b041874c3 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -1,15 +1,12 @@ -'use strict'; +import Util from './util/index'; +import MouseEvent from './mouse-event'; +import G from './g/index'; -var Util = require('./util/index'); -var MouseEvent = require('./event/mouse-event'); -var G = require('./g/index'); - -var Canvas = function(cfg) { +const Canvas = function(cfg) { Canvas.superclass.constructor.call(this, cfg); }; Canvas.CFG = { - eventEnable: true, /** * 像素宽度 @@ -181,24 +178,7 @@ Util.augment(Canvas, { * 设置初始画布参数 */ _setInitSize: function() { - if (this.get('widthStyle')) { - this.changeSizeByCss(this.get('widthStyle'), this.get('heightStyle')); - } else if (this.get('width')) { - this.changeSize(this.get('width'), this.get('height')); - } - }, - /** - * 获取像素长度 - */ - _getPx: function(edge, value) { - var canvasDOM = this.get('canvasDOM'); - canvasDOM.style[edge] = value; - var clientRect = Util.getBoundingClientRect(canvasDOM); - if (edge === 'width') { - return clientRect.right - clientRect.left; - } else if (edge === 'height') { - return clientRect.bottom - clientRect.top; - } + this.changeSize(this.get('width'), this.get('height')); }, /** * 重设画布尺寸 @@ -231,26 +211,6 @@ Util.augment(Canvas, { var height = this.get('height'); return height * pixelRatio; }, - /** - * 通过css设置画布尺寸 - * @param {String} CSS width - * @param {String} CSS height - */ - changeSizeByCss: function(width, height) { - var pixelRatio = this.get('pixelRatio'); - width = this._getPx('width', width); - height = this._getPx('height', height); - var widthCanvas = width * pixelRatio; - var heightCanvas = height * pixelRatio; - - this.set('widthStyle', width); - this.set('heightStyle', height); - this.set('widthCanvas', widthCanvas); - this.set('heightCanvas', heightCanvas); - this.set('width', width); - this.set('height', height); - this._reSize(); - }, /** * 设置画布尺寸 * @param {Number} Canvas width diff --git a/src/event/event.js b/src/event/event.js deleted file mode 100644 index 370f646d2..000000000 --- a/src/event/event.js +++ /dev/null @@ -1,42 +0,0 @@ -/** - * @fileOverview 事件基类 - # @author hankaiai@126.com 韩凯 - * @ignore - */ - -'use strict'; - -var Util = require('../util/index'); -var Event = function(type, event, bubbles, cancelable) { - this.type = type; // 事件类型 - this.target = null; // 目标 - this.currentTarget = null; // 当前目标 - this.bubbles = bubbles; // 冒泡 - this.cancelable = cancelable; // 是否能够阻止 - this.timeStamp = (new Date()).getTime(); // 时间戳 - this.defaultPrevented = false; // 阻止默认 - this.propagationStopped = false; // 阻止冒泡 - this.removed = false; // 是否被移除 - this.event = event; // 触发的原生事件 -}; - - -Util.augment(Event, { - preventDefault: function() { - this.defaultPrevented = this.cancelable && true; - }, - stopPropagation: function() { - this.propagationStopped = true; - }, - remove: function() { - this.remove = true; - }, - clone: function() { - return Util.clone(this); - }, - toString: function() { - return '[Event (type=' + this.type + ')]'; - } -}); - -module.exports = Event; diff --git a/src/g/core/element.js b/src/g/core/element.js index 0e38fd4f7..4bbf0b7b7 100644 --- a/src/g/core/element.js +++ b/src/g/core/element.js @@ -8,9 +8,8 @@ var Util = require('../../util/index'); var Attributes = require('./mixin/attributes'); var Transform = require('./mixin/transform'); var Animate = require('./mixin/animate'); +var EventDispatcher = require('./mixin/event-dispatcher'); var Format = require('../format'); -// var Vector3 = require('@ali/g-matrix').Vector3; -var EventDispatcher = require('../../event/eventDispatcher'); var SHAPE_ATTRS = [ 'fillStyle', @@ -252,7 +251,7 @@ Util.augment(Element, Attributes, EventDispatcher, Transform, Animate, { }, __setZIndex: function(zIndex) { this.__cfg.zIndex = zIndex; - if (!Util.isNull(this.get('parent'))) { + if (!Util.isNil(this.get('parent'))) { this.get('parent').sort(); } return zIndex; diff --git a/src/g/core/group.js b/src/g/core/group.js index 7fcdac6b6..f45ef7df2 100644 --- a/src/g/core/group.js +++ b/src/g/core/group.js @@ -1,5 +1,6 @@ var Util = require('../../util/index'); var Vector3 = require('@ali/g-matrix').Vector3; +// import {vec3} from 'gl-matrix'; var Element = require('./element'); var Shape = require('../shape/index'); var SHAPE_MAP = {}; // 缓存图形类型 diff --git a/src/event/eventDispatcher.js b/src/g/core/mixin/event-dispatcher.js similarity index 92% rename from src/event/eventDispatcher.js rename to src/g/core/mixin/event-dispatcher.js index 69fba4e60..436a30c46 100644 --- a/src/event/eventDispatcher.js +++ b/src/g/core/mixin/event-dispatcher.js @@ -1,5 +1,5 @@ -var Util = require('../util/index'); -var Event = require('./event'); +const Util = require('../../../util/index'); +const Event = require('@ali/g-event'); module.exports = { /** @@ -15,9 +15,9 @@ module.exports = { * @return {Object} this */ on: function(type, listener) { - var listeners = this.__listeners; + const listeners = this.__listeners; - if (Util.isNull(listeners[type])) { + if (Util.isNil(listeners[type])) { listeners[type] = []; } @@ -87,7 +87,7 @@ module.exports = { var listeners = self.__listeners; var listenersArray = listeners[event.type]; event.target = self; - if (!Util.isNull(listenersArray)) { + if (!Util.isNil(listenersArray)) { listenersArray.forEach(function(listener) { listener.call(self, event); }); diff --git a/src/g/core/shape.js b/src/g/core/shape.js index b6058bb44..e7f84c5b1 100644 --- a/src/g/core/shape.js +++ b/src/g/core/shape.js @@ -21,7 +21,7 @@ Util.augment(Shape, { var originOpacity = context.globalAlpha; if (self.hasFill()) { var fillOpacity = attrs.fillOpacity; - if (!Util.isNull(fillOpacity) && fillOpacity !== 1) { + if (!Util.isNil(fillOpacity) && fillOpacity !== 1) { context.globalAlpha = fillOpacity; context.fill(); context.globalAlpha = originOpacity; @@ -33,7 +33,7 @@ Util.augment(Shape, { var lineWidth = self.__attrs.lineWidth; if (lineWidth > 0) { var strokeOpacity = attrs.strokeOpacity; - if (!Util.isNull(strokeOpacity) && strokeOpacity !== 1) { + if (!Util.isNil(strokeOpacity) && strokeOpacity !== 1) { context.globalAlpha = strokeOpacity; } context.stroke(); diff --git a/src/g/format.js b/src/g/format.js index 2a0eea213..bceef241f 100644 --- a/src/g/format.js +++ b/src/g/format.js @@ -1,5 +1,4 @@ var Util = require('../util/index'); -var GMath = require('@ali/g-math'); var GColor = require('@ali/g-color'); var regexTags = /[MLHVQTCSAZ]([^MLHVQTCSAZ]*)/ig; @@ -35,7 +34,7 @@ function addStop(steps, gradient, opacity) { function parseLineGradient(color, self, opacity) { var arr = regexLG.exec(color); - var angle = GMath.mod(GMath.degreeToRad(parseFloat(arr[1])), Math.PI * 2); + var angle = Util.mod(Util.toRadian(parseFloat(arr[1])), Math.PI * 2); var steps = arr[2]; var box = self.getBBox(); var start; @@ -169,7 +168,7 @@ module.exports = { return parsePattern(color, self); } } - if (Util.isNull(opacity)) { + if (Util.isNil(opacity)) { return color; } return multiplyOpacity(color, opacity); diff --git a/src/g/shape/cubic.js b/src/g/shape/cubic.js index bbade62c7..b9e6dd50e 100644 --- a/src/g/shape/cubic.js +++ b/src/g/shape/cubic.js @@ -43,10 +43,10 @@ Util.augment(Cubic, { var l; if ( - Util.isNull(p1) || - Util.isNull(p2) || - Util.isNull(p3) || - Util.isNull(p4) + Util.isNil(p1) || + Util.isNil(p2) || + Util.isNil(p3) || + Util.isNil(p4) ) { return null; } @@ -96,10 +96,10 @@ Util.augment(Cubic, { var arrow = attrs.arrow; context = context || self.get('context'); if ( - Util.isNull(p1) || - Util.isNull(p2) || - Util.isNull(p3) || - Util.isNull(p4) + Util.isNil(p1) || + Util.isNil(p2) || + Util.isNil(p3) || + Util.isNil(p4) ) { return; } diff --git a/src/g/shape/fan.js b/src/g/shape/fan.js index e7cb00bd5..bfc45ec89 100644 --- a/src/g/shape/fan.js +++ b/src/g/shape/fan.js @@ -7,7 +7,6 @@ var Util = require('../../util/index'); var Shape = require('../core/shape'); var Inside = require('./util/inside'); -var gMath = require('@ali/g-math'); var ArcMath = require('./math/arc'); var Matrix = require('@ali/g-matrix'); var Vector2 = Matrix.Vector2; @@ -102,7 +101,7 @@ Util.augment(Fan, { var angle1 = ArcMath.nearAngle(angle, startAngle, endAngle, clockwise); - if (gMath.equal(angle, angle1)) { + if (Util.isNumberEqual(angle, angle1)) { var ls = subv.lengthSq(); if (rs * rs <= ls && ls <= re * re) { return true; diff --git a/src/g/shape/image.js b/src/g/shape/image.js index e374d8198..8f2b04737 100644 --- a/src/g/shape/image.js +++ b/src/g/shape/image.js @@ -142,19 +142,19 @@ Util.augment(CImage, { if (img instanceof Image || (img instanceof HTMLElement && Util.isString(img.nodeName) && img.nodeName.toUpperCase() === 'CANVAS')) { if ( - Util.isNull(sx) || - Util.isNull(sy) || - Util.isNull(swidth) || - Util.isNull(sheight) + Util.isNil(sx) || + Util.isNil(sy) || + Util.isNil(swidth) || + Util.isNil(sheight) ) { context.drawImage(img, x, y, width, height); return; } if ( - !Util.isNull(sx) && - !Util.isNull(sy) && - !Util.isNull(swidth) && - !Util.isNull(sheight) + !Util.isNil(sx) && + !Util.isNil(sy) && + !Util.isNil(swidth) && + !Util.isNil(sheight) ) { context.drawImage(img, sx, sy, swidth, sheight, x, y, width, height); return; diff --git a/src/g/shape/math/arc.js b/src/g/shape/math/arc.js index a00b9e7ab..03736e925 100644 --- a/src/g/shape/math/arc.js +++ b/src/g/shape/math/arc.js @@ -4,7 +4,6 @@ * @ignore */ var Vector2 = require('@ali/g-matrix').Vector2; -var GMath = require('@ali/g-math'); var Util = require('../../../util/index'); function circlePoint(cx, cy, r, angle) { @@ -38,9 +37,9 @@ function nearAngle(angle, startAngle, endAngle, clockwise) { if (endAngle - startAngle >= Math.PI * 2) { plus = Math.PI * 2; } - startAngle = GMath.mod(startAngle, Math.PI * 2); - endAngle = GMath.mod(endAngle, Math.PI * 2) + plus; - angle = GMath.mod(angle, Math.PI * 2); + startAngle = Util.mod(startAngle, Math.PI * 2); + endAngle = Util.mod(endAngle, Math.PI * 2) + plus; + angle = Util.mod(angle, Math.PI * 2); if (clockwise) { if (startAngle >= endAngle) { if (angle > endAngle && angle < startAngle) { diff --git a/src/g/shape/math/cubic.js b/src/g/shape/math/cubic.js index 72bdc8386..319e2ed55 100644 --- a/src/g/shape/math/cubic.js +++ b/src/g/shape/math/cubic.js @@ -4,7 +4,6 @@ * @ignore */ var Vector2 = require('@ali/g-matrix').Vector2; -var GMath = require('@ali/g-math'); var Util = require('../../../util/index'); function cubicAt(p0, p1, p2, p3, t) { @@ -101,8 +100,8 @@ function cubicExtrema(p0, p1, p2, p3) { var t2; var discSqrt; - if (GMath.equal(a, 0)) { - if (!GMath.equal(b, 0)) { + if (Util.isNumberEqual(a, 0)) { + if (!Util.isNumberEqual(b, 0)) { t1 = -c / b; if (t1 >= 0 && t1 <= 1) { extrema.push(t1); @@ -110,7 +109,7 @@ function cubicExtrema(p0, p1, p2, p3) { } } else { var disc = b * b - 4 * a * c; - if (GMath.equal(disc, 0)) { + if (Util.isNumberEqual(disc, 0)) { extrema.push(-b / (2 * a)); } else if (disc > 0) { discSqrt = Math.sqrt(disc); @@ -134,7 +133,7 @@ function base3(t, p1, p2, p3, p4) { } function cubiclLen(x1, y1, x2, y2, x3, y3, x4, y4, z) { - if (Util.isNull(z)) { + if (Util.isNil(z)) { z = 1; } z = z > 1 ? 1 : z < 0 ? 0 : z; diff --git a/src/g/shape/math/quadratic.js b/src/g/shape/math/quadratic.js index cb746ce13..dbc2a29ba 100644 --- a/src/g/shape/math/quadratic.js +++ b/src/g/shape/math/quadratic.js @@ -4,7 +4,7 @@ * @ignore */ var Vector2 = require('@ali/g-matrix').Vector2; -var GMath = require('@ali/g-math'); +var Util = require('../../../util/index'); function quadraticAt(p0, p1, p2, t) { var onet = 1 - t; @@ -84,7 +84,7 @@ function quadraticProjectPoint(x1, y1, x2, y2, x3, y3, x, y, out) { function quadraticExtrema(p0, p1, p2) { var a = p0 + p2 - 2 * p1; - if (GMath.equal(a, 0)) { + if (Util.isNumberEqual(a, 0)) { return [0.5]; } var rst = (p0 - p1) / a; diff --git a/src/g/shape/path.js b/src/g/shape/path.js index 95e61f104..70f896f08 100644 --- a/src/g/shape/path.js +++ b/src/g/shape/path.js @@ -39,7 +39,7 @@ Util.augment(Path, { }, __afterSetAttrPath: function(path) { var self = this; - if (Util.isNull(path)) { + if (Util.isNil(path)) { self.setSilent('segments', null); self.setSilent('box', undefined); return; @@ -230,7 +230,7 @@ Util.augment(Path, { } }); seg = curve[index]; - if (Util.isNull(seg) || Util.isNull(index)) { + if (Util.isNil(seg) || Util.isNil(index)) { return null; } l = seg.length; diff --git a/src/g/shape/quadratic.js b/src/g/shape/quadratic.js index 79efed28c..6f62f6a51 100644 --- a/src/g/shape/quadratic.js +++ b/src/g/shape/quadratic.js @@ -43,9 +43,9 @@ Util.augment(Quadratic, { var l; if ( - Util.isNull(p1) || - Util.isNull(p2) || - Util.isNull(p3) + Util.isNil(p1) || + Util.isNil(p2) || + Util.isNil(p3) ) { return null; } @@ -95,9 +95,9 @@ Util.augment(Quadratic, { var arrow = attrs.arrow; if ( - Util.isNull(p1) || - Util.isNull(p2) || - Util.isNull(p3) + Util.isNil(p1) || + Util.isNil(p2) || + Util.isNil(p3) ) { return; } diff --git a/src/g/shape/text.js b/src/g/shape/text.js index 1fab54cdc..5631b3edb 100644 --- a/src/g/shape/text.js +++ b/src/g/shape/text.js @@ -209,7 +209,7 @@ Util.augment(CText, { context.beginPath(); if (self.hasFill()) { var fillOpacity = attrs.fillOpacity; - if (!Util.isNull(fillOpacity) && fillOpacity !== 1) { + if (!Util.isNil(fillOpacity) && fillOpacity !== 1) { context.globalAlpha = fillOpacity; } if (textArr) { @@ -246,7 +246,7 @@ Util.augment(CText, { var measureWidth; var width = 0; - if (Util.isNull(text)) return undefined; + if (Util.isNil(text)) return undefined; var context = Common.backupContext; context.save(); context.font = font; diff --git a/src/g/shape/util/pathSegment.js b/src/g/shape/util/pathSegment.js index f97c7dfc1..d2914d529 100644 --- a/src/g/shape/util/pathSegment.js +++ b/src/g/shape/util/pathSegment.js @@ -5,7 +5,6 @@ * @ignore */ var Util = require('../../../util/index'); -var GMath = require('@ali/g-math'); var Inside = require('./inside'); var Cubic = require('../math/cubic'); var Quadratic = require('../math/quadratic'); @@ -45,7 +44,7 @@ function vAngle(u, v) { } function getArcParams(point1, point2, fa, fs, rx, ry, psiDeg) { - var psi = GMath.mod(GMath.degreeToRad(psiDeg), Math.PI * 2); + var psi = Util.mod(Util.toRadian(psiDeg), Math.PI * 2); var x1 = point1.x; var y1 = point1.y; var x2 = point2.x; diff --git a/src/event/mouse-event.js b/src/mouse-event.js similarity index 74% rename from src/event/mouse-event.js rename to src/mouse-event.js index 080cc1bbe..4cff126cb 100644 --- a/src/event/mouse-event.js +++ b/src/mouse-event.js @@ -1,15 +1,7 @@ -/** - * @fileOverview mouse 事件 - * @author hankaiai@126.com - * @ignore - */ +import Util from './util/index'; +import Event from '@ali/g-event'; -'use strict'; - -var Util = require('../util/index'); -var Event = require('@ali/g-event'); - -var MouseEvent = function(canvas) { +const MouseEvent = function(canvas) { this.canvas = canvas; this.el = canvas.get('el'); this.current = null; @@ -25,18 +17,18 @@ Util.augment(MouseEvent, { } }, getCurrent: function(e) { - var canvas = this.canvas; - var point = canvas.getPointByClient(e.clientX, e.clientY); + const canvas = this.canvas; + const point = canvas.getPointByClient(e.clientX, e.clientY); this.point = point; this.pre = this.current; this.current = canvas.getShape(point.x, point.y); }, mousemove: function(e) { this.getCurrent(e); - var point = this.point; - var canvas = this.canvas; + const point = this.point; + const canvas = this.canvas; if (canvas.has('canvas-mousemove')) { - var canvasmousemove = new Event('canvas-mousemove', e, true, true); + const canvasmousemove = new Event('canvas-mousemove', e, true, true); canvasmousemove.x = point.x; canvasmousemove.y = point.y; canvasmousemove.clientX = e.clientX; @@ -46,7 +38,7 @@ Util.augment(MouseEvent, { } if (this.pre && this.pre !== this.current) { - var mouseleave = new Event('mouseleave', e, true, true); + const mouseleave = new Event('mouseleave', e, true, true); mouseleave.x = point.x; mouseleave.y = point.y; mouseleave.clientX = e.clientX; @@ -57,7 +49,7 @@ Util.augment(MouseEvent, { } if (this.current) { - var mousemove = new Event('mousemove', e, true, true); + const mousemove = new Event('mousemove', e, true, true); mousemove.x = point.x; mousemove.y = point.y; mousemove.clientX = e.clientX; @@ -67,7 +59,7 @@ Util.augment(MouseEvent, { this.tryTrigger(this.current, mousemove); if (this.pre !== this.current) { - var mouseenter = new Event('mouseenter', e, true, true); + const mouseenter = new Event('mouseenter', e, true, true); mouseenter.x = point.x; mouseenter.y = point.y; mouseenter.clientX = e.clientX; @@ -79,11 +71,11 @@ Util.augment(MouseEvent, { } }, mousedown: function(e) { - var point = this.point; - var canvas = this.canvas; + const point = this.point; + const canvas = this.canvas; if (canvas.has('canvas-mousedown')) { - var canvasmousedown = new Event('canvas-mousedown', e, true, true); + const canvasmousedown = new Event('canvas-mousedown', e, true, true); canvasmousedown.x = point.x; canvasmousedown.y = point.y; canvasmousedown.clientX = e.clientX; @@ -94,7 +86,7 @@ Util.augment(MouseEvent, { if (this.current) { - var mousedown = new Event('mousedown', e, true, true); + const mousedown = new Event('mousedown', e, true, true); mousedown.x = point.x; mousedown.y = point.y; mousedown.clientX = e.clientX; @@ -105,10 +97,10 @@ Util.augment(MouseEvent, { } }, mouseup: function(e) { - var point = this.point; - var canvas = this.canvas; + const point = this.point; + const canvas = this.canvas; if (canvas.has('canvas-mouseup')) { - var canvasmouseup = new Event('canvas-mouseup', e, true, true); + const canvasmouseup = new Event('canvas-mouseup', e, true, true); canvasmouseup.x = point.x; canvasmouseup.y = point.y; canvasmouseup.clientX = e.clientX; @@ -117,7 +109,7 @@ Util.augment(MouseEvent, { this.tryTrigger(canvas, canvasmouseup); } if (this.current) { - var mouseup = new Event('mouseup', e, true, true); + const mouseup = new Event('mouseup', e, true, true); mouseup.x = point.x; mouseup.y = point.y; mouseup.clientX = e.clientX; @@ -129,10 +121,10 @@ Util.augment(MouseEvent, { }, click: function(e) { this.getCurrent(e); - var point = this.point; - var canvas = this.canvas; + const point = this.point; + const canvas = this.canvas; if (canvas.has('canvas-click')) { - var canvasclick = new Event('canvas-click', e, true, true); + const canvasclick = new Event('canvas-click', e, true, true); canvasclick.x = point.x; canvasclick.y = point.y; canvasclick.clientX = e.clientX; @@ -142,7 +134,7 @@ Util.augment(MouseEvent, { } if (this.current) { - var click = new Event('click', e, true, true); + const click = new Event('click', e, true, true); click.x = point.x; click.y = point.y; click.clientX = e.clientX; @@ -153,11 +145,11 @@ Util.augment(MouseEvent, { } }, dblclick: function(e) { - var point = this.point; - var canvas = this.canvas; + const point = this.point; + const canvas = this.canvas; if (canvas.has('canvas-dblclick')) { - var canvasdblclick = new Event('canvas-dblclick', e, true, true); + const canvasdblclick = new Event('canvas-dblclick', e, true, true); canvasdblclick.x = point.x; canvasdblclick.y = point.y; canvasdblclick.clientX = e.clientX; @@ -168,7 +160,7 @@ Util.augment(MouseEvent, { if (this.current) { - var dblclick = new Event('dblclick', e, true, true); + const dblclick = new Event('dblclick', e, true, true); dblclick.x = point.x; dblclick.y = point.y; dblclick.clientX = e.clientX; @@ -179,19 +171,19 @@ Util.augment(MouseEvent, { } }, mouseout: function(e) { - var point = this.point; - var canvas = this.canvas; + const point = this.point; + const canvas = this.canvas; - var canvasmouseleave = new Event('canvas-mouseleave', e, true, true); + const canvasmouseleave = new Event('canvas-mouseleave', e, true, true); canvasmouseleave.x = point.x; canvasmouseleave.y = point.y; canvasmouseleave.currentTarget = canvas; this.tryTrigger(canvas, canvasmouseleave); }, mouseover: function(e) { - var canvas = this.canvas; + const canvas = this.canvas; - var canvasmouseenter = new Event('canvas-mouseenter', e, true, true); + const canvasmouseenter = new Event('canvas-mouseenter', e, true, true); canvasmouseenter.currentTarget = canvas; this.tryTrigger(canvas, canvasmouseenter); } diff --git a/src/util/common-util.js b/src/util/common.js similarity index 65% rename from src/util/common-util.js rename to src/util/common.js index 2beb2ec6f..636a9fa0b 100644 --- a/src/util/common-util.js +++ b/src/util/common.js @@ -2,8 +2,7 @@ import { isFunction, isObject, isBoolean, - isNull, - isUndefined, + isNil, isString, isArray, isEmpty, @@ -17,13 +16,15 @@ import { toArray, } from 'lodash'; +const PRECISION = 0.00001; // 常量,据的精度,小于这个精度认为是0 +const RADIAN = Math.PI / 180; +const DEGREE = 180 / Math.PI; + module.exports = { isFunction, isObject, isBoolean, - isNull(param) { - return isNull(param) || isUndefined(param); - }, + isNil, isString, isArray, isEmpty, // isBlank @@ -76,4 +77,37 @@ module.exports = { merge(c.prototype, obj); } }, + /** + * 判断两个数是否相等 + * @param {Number} a 数 + * @param {Number} b 数 + * @return {Boolean} 是否相等 + **/ + isNumberEqual(a, b) { + return Math.abs((a - b)) < PRECISION; + }, + /** + * 获取角度对应的弧度 + * @param {Number} degree 角度 + * @return {Number} 弧度 + **/ + toRadian(degree) { + return RADIAN * degree; + }, + /** + * 获取弧度对应的角度 + * @param {Number} rad 弧度 + * @return {Number} 角度 + **/ + toDegree(radian) { + return DEGREE * radian; + }, + /** + * 广义取模运算 + * @param {Number} v 被取模的值 + * @param {Number} m 模 + */ + mod(n, m) { + return ( ( n % m ) + m ) % m; + } }; diff --git a/src/util/dom-util.js b/src/util/dom.js similarity index 100% rename from src/util/dom-util.js rename to src/util/dom.js diff --git a/src/util/index.js b/src/util/index.js index 3bdc44d60..ec8c06d90 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -1,5 +1,5 @@ -import CommonUtil from './common-util'; -import DomUtil from './dom-util'; +import CommonUtil from './common'; +import DomUtil from './dom'; const Util = {}; diff --git a/src/util/matrix-util.js b/src/util/matrix.js similarity index 100% rename from src/util/matrix-util.js rename to src/util/matrix.js diff --git a/tests/canvas/canvas-spec.js b/tests/canvas/canvas-spec.js index 9cbea2774..3519c816d 100644 --- a/tests/canvas/canvas-spec.js +++ b/tests/canvas/canvas-spec.js @@ -37,9 +37,6 @@ describe('Layer', function() { canvas.changeSize(200, 200); }); - it('通过CSS 改变尺寸 change size by css', function() { - canvas.changeSizeByCss('10em', '10em'); - }); it('清空图层 clear layer', function() { canvas.clear(); diff --git a/tests/g/core-eventDispatcher-spec.js b/tests/g/core-eventDispatcher-spec.js index 4ca1a726d..acdee1311 100644 --- a/tests/g/core-eventDispatcher-spec.js +++ b/tests/g/core-eventDispatcher-spec.js @@ -1,13 +1,12 @@ var expect = require('@ali/expect.js'); var Util = require('../../src/util/index'); -var Dispatcher = require('../../src/event/eventDispatcher'); +var Dispatcher = require('../../src/g/core/mixin/event-dispatcher'); var Element = function() { this.initEventDispatcher(); }; Util.augment(Element, Dispatcher); - describe('EventDispatcher', function() { it('trigger', function() { diff --git a/tests/matrixUtil/matrixUtil-spec.js b/tests/matrixUtil/matrixUtil-spec.js index bd0256e91..b41241f89 100644 --- a/tests/matrixUtil/matrixUtil-spec.js +++ b/tests/matrixUtil/matrixUtil-spec.js @@ -1,7 +1,7 @@ var $ = require('jquery'); var expect = require('@ali/expect.js'); -var matrixUtil = require('../../src/util/matrix-util'); +var matrixUtil = require('../../src/util/matrix'); var Matrix = require('@ali/g-matrix'); var Matrix3 = Matrix.Matrix3; var Canvas = require('../../src/canvas'); diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 000000000..0b7c6e2ae --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,18 @@ +var webpack = require('atool-build/lib/webpack'); + +module.exports = function(webpackConfig) { + + webpackConfig.plugins.some(function(plugin, i){ + if(plugin instanceof webpack.optimize.CommonsChunkPlugin) { + webpackConfig.plugins.splice(i, 1); + + return true; + } + }); + + webpackConfig.output.library = 'G'; + webpackConfig.output.libraryTarget = 'var'; + // webpackConfig.entry=['./tests/debug.js']; + + return webpackConfig; +};