一大波精简

master
yutent 2024-03-07 15:42:22 +08:00
parent 138681c92f
commit 15de4b4cdd
11 changed files with 363 additions and 529 deletions

View File

View File

@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
import eve from './eve.js' import eve from './eve.js'
import { Snap, SnapElement, Paper, Fragment } from './svg.js' import { SnapElement } from './svg.js'
import mina from './mina.js' import mina from './mina.js'
import { is } from './utils.js' import { is } from './utils.js'
@ -39,89 +39,80 @@ let Animation = function (attr, ms, easing, callback) {
easing && (this.easing = easing) easing && (this.easing = easing)
callback && (this.callback = callback) callback && (this.callback = callback)
} }
Snap._.Animation = Animation export { Animation }
/*\ /**
* Snap.animation * Creates an animation object
[ method ] **
** * attr (object) attributes of final destination
* Creates an animation object * duration (number) duration of the animation, in milliseconds
** * easing (function) #optional one of easing functions of @mina or custom one
- attr (object) attributes of final destination * callback (function) #optional callback function that fires when animation ends
- duration (number) duration of the animation, in milliseconds */
- easing (function) #optional one of easing functions of @mina or custom one export function createAnimation(attr, ms, easing, callback) {
- callback (function) #optional callback function that fires when animation ends
= (object) animation object
\*/
Snap.animation = function (attr, ms, easing, callback) {
return new Animation(attr, ms, easing, callback) return new Animation(attr, ms, easing, callback)
} }
/*\ /**
* SnapElement.inAnim * Returns a set of animations that may be able to manipulate the current element
[ method ] **
** = (object) in format:
* Returns a set of animations that may be able to manipulate the current element {
** anim (object) animation object,
= (object) in format: mina (object) @mina object,
o { curStatus (number) 0..1 status of the animation: 0 just started, 1 just finished,
o anim (object) animation object, status (function) gets or sets the status of the animation,
o mina (object) @mina object, stop (function) stops the animation
o curStatus (number) 0..1 status of the animation: 0 just started, 1 just finished, }
o status (function) gets or sets the status of the animation, */
o stop (function) stops the animation
o }
\*/
elproto.inAnim = function () { elproto.inAnim = function () {
let el = this, let el = this
res = [] let res = []
for (let id in el.anims)
if (el.anims[has](id)) { for (let id in el.anims) {
;(function (a) { if (el.anims.hasOwnProperty(id)) {
res.push({ let a = el.anims[id]
anim: new Animation(a._attrs, a.dur, a.easing, a._callback), res.push({
mina: a, anim: new Animation(a._attrs, a.dur, a.easing, a._callback),
curStatus: a.status(), mina: a,
status: function (val) { curStatus: a.status(),
return a.status(val) status(val) {
}, return a.status(val)
stop: function () { },
a.stop() stop() {
} a.stop()
}) }
})(el.anims[id]) })
} }
}
return res return res
} }
/*\ /**
* Snap.animate * Runs generic animation of one number into another with a caring function
[ method ] **
** - from (number|array) number or array of numbers
* Runs generic animation of one number into another with a caring function - to (number|array) number or array of numbers
** - setter (function) caring function that accepts one number argument
- from (number|array) number or array of numbers - duration (number) duration, in milliseconds
- to (number|array) number or array of numbers - easing (function) #optional easing function from @mina or custom
- setter (function) caring function that accepts one number argument - callback (function) #optional callback function to execute when animation ends
- duration (number) duration, in milliseconds = (object) animation object in @mina format
- easing (function) #optional easing function from @mina or custom {
- callback (function) #optional callback function to execute when animation ends id (string) animation id, consider it read-only,
= (object) animation object in @mina format duration (function) gets or sets the duration of the animation,
o { easing (function) easing,
o id (string) animation id, consider it read-only, speed (function) gets or sets the speed of the animation,
o duration (function) gets or sets the duration of the animation, status (function) gets or sets the status of the animation,
o easing (function) easing, stop (function) stops the animation
o speed (function) gets or sets the speed of the animation, }
o status (function) gets or sets the status of the animation, let rect = new Snap().rect(0, 0, 10, 10);
o stop (function) stops the animation animate(0, 10, function (val) {
o } rect.attr({
| let rect = new Snap().rect(0, 0, 10, 10); x: val
| Snap.animate(0, 10, function (val) { });
| rect.attr({ }, 1000);
| x: val // in given context is equivalent to
| }); rect.animate({x: 10}, 1000);
| }, 1000); */
| // in given context is equivalent to export function animate(from, to, setter, ms, easing, callback) {
| rect.animate({x: 10}, 1000);
\*/
Snap.animate = function (from, to, setter, ms, easing, callback) {
if (typeof easing == 'function' && !easing.length) { if (typeof easing == 'function' && !easing.length) {
callback = easing callback = easing
easing = mina.linear easing = mina.linear

View File

@ -1,18 +1,10 @@
// Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. /**
// * {}
// Licensed under the Apache License, Version 2.0 (the "License"); * @author yutent<yutent.io@gmail.com>
// you may not use this file except in compliance with the License. * @date 2024/03/07 15:17:00
// You may obtain a copy of the License at */
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import eve from './eve.js' import eve from './eve.js'
import { Snap, SnapElement, Paper, Fragment } from './svg.js'
let operators = { let operators = {
'+': function (x, y) { '+': function (x, y) {
@ -51,7 +43,7 @@ eve.on('snap.util.attr', function (val) {
aUnit = a.match(reUnit), aUnit = a.match(reUnit),
op = operators[plus[1]] op = operators[plus[1]]
if (aUnit && aUnit == unit) { if (aUnit && aUnit == unit) {
val = op(parseFloat(a), +plus[2]) val = op(+a, +plus[2])
} else { } else {
a = this.asPX(name) a = this.asPX(name)
val = op(this.asPX(name), this.asPX(name, plus[2] + unit)) val = op(this.asPX(name), this.asPX(name, plus[2] + unit))
@ -63,6 +55,7 @@ eve.on('snap.util.attr', function (val) {
this.attr(atr) this.attr(atr)
} }
})(-10) })(-10)
eve.on('snap.util.equal', function (name, b) { eve.on('snap.util.equal', function (name, b) {
let A, let A,
B, B,
@ -76,8 +69,8 @@ eve.on('snap.util.equal', function (name, b) {
op = operators[bplus[1]] op = operators[bplus[1]]
if (aUnit && aUnit == unit) { if (aUnit && aUnit == unit) {
return { return {
from: parseFloat(a), from: +a,
to: op(parseFloat(a), +bplus[2]), to: op(+a, +bplus[2]),
f: getUnit(aUnit) f: getUnit(aUnit)
} }
} else { } else {

View File

@ -23,6 +23,7 @@ import {
unit2px unit2px
} from './svg.js' } from './svg.js'
import { $, is } from './utils.js' import { $, is } from './utils.js'
import { Matrix } from './matrix.js'
let elproto = SnapElement.prototype, let elproto = SnapElement.prototype,
Str = String, Str = String,
@ -55,11 +56,11 @@ elproto.getBBox = function (isWithoutTransform) {
if (this.type == 'tspan') { if (this.type == 'tspan') {
return Snap._.box(this.node.getClientRects().item(0)) return Snap._.box(this.node.getClientRects().item(0))
} }
if (!Snap.Matrix || !Snap.path) { if (!Snap.path) {
return this.node.getBBox() return this.node.getBBox()
} }
let el = this, let el = this,
m = new Snap.Matrix() m = new Matrix()
if (el.removed) { if (el.removed) {
return Snap._.box() return Snap._.box()
} }
@ -114,7 +115,7 @@ function extractTransform(el, tstr) {
tstr = el.node.getAttribute('transform') tstr = el.node.getAttribute('transform')
} }
if (!tstr) { if (!tstr) {
return new Snap.Matrix() return new Matrix()
} }
tstr = Snap._.svgTransform2string(tstr) tstr = Snap._.svgTransform2string(tstr)
} else { } else {
@ -159,10 +160,10 @@ elproto.transform = function (tstr) {
let _ = this._ let _ = this._
if (tstr == null) { if (tstr == null) {
let papa = this, let papa = this,
global = new Snap.Matrix(this.node.getCTM()), global = new Matrix(this.node.getCTM()),
local = extractTransform(this), local = extractTransform(this),
ms = [local], ms = [local],
m = new Snap.Matrix(), m = new Matrix(),
i, i,
localString = local.toTransformString(), localString = local.toTransformString(),
string = Str(local) == Str(this.matrix) ? Str(_.transform) : localString string = Str(local) == Str(this.matrix) ? Str(_.transform) : localString
@ -185,7 +186,7 @@ elproto.transform = function (tstr) {
toString: propString toString: propString
} }
} }
if (tstr instanceof Snap.Matrix) { if (tstr instanceof Matrix) {
this.matrix = tstr this.matrix = tstr
this._.transform = tstr.toTransformString() this._.transform = tstr.toTransformString()
} else { } else {
@ -433,7 +434,7 @@ elproto.select = function (query) {
\*/ \*/
elproto.selectAll = function (query) { elproto.selectAll = function (query) {
let nodelist = this.node.querySelectorAll(query), let nodelist = this.node.querySelectorAll(query),
set = (Snap.set || Array)() set = []
for (let i = 0; i < nodelist.length; i++) { for (let i = 0; i < nodelist.length; i++) {
set.push(wrap(nodelist[i])) set.push(wrap(nodelist[i]))
} }

View File

@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import eve from './eve.js' import eve from './eve.js'
import { Snap, SnapElement, Paper, Fragment } from './svg.js' import { Snap } from './svg.js'
import { is } from './utils.js' import { is } from './utils.js'
import { SEPARATOR } from './lib/constants.js' import { SEPARATOR } from './lib/constants.js'
import { parseColor } from './lib/color.js' import { parseColor } from './lib/color.js'
import { Matrix } from './matrix.js'
let names = {}, let names = {},
reUnit = /[%a-z]+$/i, reUnit = /[%a-z]+$/i,
@ -45,8 +46,8 @@ function getEmpty(item) {
} }
} }
function equaliseTransform(t1, t2, getBBox) { function equaliseTransform(t1, t2, getBBox) {
t1 = t1 || new Snap.Matrix() t1 = t1 || new Matrix()
t2 = t2 || new Snap.Matrix() t2 = t2 || new Matrix()
t1 = Snap.parseTransformString(t1.toTransformString()) || [] t1 = Snap.parseTransformString(t1.toTransformString()) || []
t2 = Snap.parseTransformString(t2.toTransformString()) || [] t2 = Snap.parseTransformString(t2.toTransformString()) || []
let maxlength = Math.max(t1.length, t2.length), let maxlength = Math.max(t1.length, t2.length),
@ -136,9 +137,7 @@ function arrayEqual(arr1, arr2) {
} }
return arr1.toString() == arr2.toString() return arr1.toString() == arr2.toString()
} }
SnapElement.prototype.equal = function (name, b) {
return eve('snap.util.equal', this, name, b).firstDefined()
}
eve.on('snap.util.equal', function (name, b) { eve.on('snap.util.equal', function (name, b) {
let A, let A,
B, B,

View File

@ -12,12 +12,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import eve from './eve.js' import eve from './eve.js'
import { Snap, SnapElement, Paper, Fragment } from './svg.js' import { Snap, SnapElement } from './svg.js'
import { $ } from './utils.js' import { $ } from './utils.js'
import { parseColor } from './lib/color.js' import { parseColor } from './lib/color.js'
let elproto = SnapElement.prototype, let rgurl = /^\s*url\((.+)\)/,
rgurl = /^\s*url\((.+)\)/,
Str = String Str = String
Snap.filter = {} Snap.filter = {}

View File

@ -1,8 +1,8 @@
import { Snap } from './svg.js' import { Snap } from './svg.js'
import './paper.js' import './paper.js'
import './element.js' import './element.js'
import './animation.js' import { createAnimation, animate } from './animation.js'
import './matrix.js' import { Matrix } from './matrix.js'
import './attr.js' import './attr.js'
import './attradd.js' import './attradd.js'
import './path.js' import './path.js'
@ -12,3 +12,9 @@ import './mouse.js'
import './filter.js' import './filter.js'
export default Snap export default Snap
export function createSvg(...args) {
return new Snap(...args)
}
export { Matrix, createAnimation, animate }

View File

@ -1,63 +1,53 @@
// Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. /**
// * {}
// Licensed under the Apache License, Version 2.0 (the "License"); * @author yutent<yutent.io@gmail.com>
// you may not use this file except in compliance with the License. * @date 2024/03/07 14:41:43
// You may obtain a copy of the License at */
//
// http://www.apache.org/licenses/LICENSE-2.0 import { is } from './utils.js'
// import { rad, deg } from './lib/math.js'
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, function norm(a) {
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. return a[0] * a[0] + a[1] * a[1]
// See the License for the specific language governing permissions and
// limitations under the License.
import { Snap, SnapElement, Paper, Fragment } from './svg.js'
let objectToString = Object.prototype.toString,
Str = String,
math = Math,
E = ''
function Matrix(a, b, c, d, e, f) {
if (b == null && objectToString.call(a) == '[object SVGMatrix]') {
this.a = a.a
this.b = a.b
this.c = a.c
this.d = a.d
this.e = a.e
this.f = a.f
return
}
if (a != null) {
this.a = +a
this.b = +b
this.c = +c
this.d = +d
this.e = +e
this.f = +f
} else {
this.a = 1
this.b = 0
this.c = 0
this.d = 1
this.e = 0
this.f = 0
}
} }
;(function (matrixproto) { function normalize(a) {
/*\ let mag = Math.sqrt(norm(a))
* Matrix.add a[0] && (a[0] /= mag)
[ method ] a[1] && (a[1] /= mag)
** }
* Adds the given matrix to existing one
- a (number) export class Matrix {
- b (number) a = 1
- c (number) b = 0
- d (number) c = 0
- e (number) d = 1
- f (number) e = 0
* or f = 0
- matrix (object) @Matrix
\*/ constructor(a, b, c, d, e, f) {
matrixproto.add = function (a, b, c, d, e, f) { if (b === void 0 && is(a, 'SVGMatrix')) {
this.a = a.a
this.b = a.b
this.c = a.c
this.d = a.d
this.e = a.e
this.f = a.f
return
}
if (a !== void 0) {
this.a = +a
this.b = +b
this.c = +c
this.d = +d
this.e = +e
this.f = +f
}
}
/**
* Adds the given matrix to existing one
*/
add(a, b, c, d, e, f) {
if (a && a instanceof Matrix) { if (a && a instanceof Matrix) {
return this.add(a.a, a.b, a.c, a.d, a.e, a.f) return this.add(a.a, a.b, a.c, a.d, a.e, a.f)
} }
@ -72,21 +62,11 @@ function Matrix(a, b, c, d, e, f) {
this.b = bNew this.b = bNew
return this return this
} }
/*\
* Matrix.multLeft /**
[ method ] * Multiplies a passed affine transform to the left: M * this.
** */
* Multiplies a passed affine transform to the left: M * this. multLeft(a, b, c, d, e, f) {
- a (number)
- b (number)
- c (number)
- d (number)
- e (number)
- f (number)
* or
- matrix (object) @Matrix
\*/
Matrix.prototype.multLeft = function (a, b, c, d, e, f) {
if (a && a instanceof Matrix) { if (a && a instanceof Matrix) {
return this.multLeft(a.a, a.b, a.c, a.d, a.e, a.f) return this.multLeft(a.a, a.b, a.c, a.d, a.e, a.f)
} }
@ -102,16 +82,13 @@ function Matrix(a, b, c, d, e, f) {
this.e = eNew this.e = eNew
return this return this
} }
/*\
* Matrix.invert /**
[ method ] * Returns an inverted version of the matrix
** */
* Returns an inverted version of the matrix invert() {
= (object) @Matrix let me = this
\*/ let x = me.a * me.d - me.b * me.c
matrixproto.invert = function () {
let me = this,
x = me.a * me.d - me.b * me.c
return new Matrix( return new Matrix(
me.d / x, me.d / x,
-me.b / x, -me.b / x,
@ -121,41 +98,76 @@ function Matrix(a, b, c, d, e, f) {
(me.b * me.e - me.a * me.f) / x (me.b * me.e - me.a * me.f) / x
) )
} }
/*\ /**
* Matrix.clone * Returns a copy of the matrix
[ method ] */
** clone() {
* Returns a copy of the matrix
= (object) @Matrix
\*/
matrixproto.clone = function () {
return new Matrix(this.a, this.b, this.c, this.d, this.e, this.f) return new Matrix(this.a, this.b, this.c, this.d, this.e, this.f)
} }
/*\ /**
* Matrix.translate * Translate the matrix
[ method ] * x (number) horizontal offset distance
** * y (number) vertical offset distance
* Translate the matrix */
- x (number) horizontal offset distance translate(x, y) {
- y (number) vertical offset distance
\*/
matrixproto.translate = function (x, y) {
this.e += x * this.a + y * this.c this.e += x * this.a + y * this.c
this.f += x * this.b + y * this.d this.f += x * this.b + y * this.d
return this return this
} }
/*\
* Matrix.scale /**
[ method ] * Rotates the matrix
** * a (number) angle of rotation, in degrees
* Scales the matrix * x (number) horizontal origin point from which to rotate
- x (number) amount to be scaled, with `1` resulting in no change * y (number) vertical origin point from which to rotate
- y (number) #optional amount to scale along the vertical axis. (Otherwise `x` applies to both axes.) */
- cx (number) #optional horizontal origin point from which to scale rotate(a, x, y) {
- cy (number) #optional vertical origin point from which to scale a = rad(a)
* Default cx, cy is the middle point of the element. x = x || 0
\*/ y = y || 0
matrixproto.scale = function (x, y, cx, cy) { let cos = +Math.cos(a).toFixed(9),
sin = +Math.sin(a).toFixed(9)
this.add(cos, sin, -sin, cos, x, y)
return this.add(1, 0, 0, 1, -x, -y)
}
/**
* Skews the matrix along the x-axis
* x (number) Angle to skew along the x-axis (in degrees).
*/
skewX(x) {
return this.skew(x, 0)
}
/**
* Skews the matrix along the y-axis
* y (number) Angle to skew along the y-axis (in degrees).
*/
skewY(y) {
return this.skew(0, y)
}
/**
* Skews the matrix
* y (number) Angle to skew along the y-axis (in degrees).
* x (number) Angle to skew along the x-axis (in degrees).
*/
skew(x, y) {
x = x || 0
y = y || 0
x = rad(x)
y = rad(y)
let c = Math.tan(x).toFixed(9)
let b = Math.tan(y).toFixed(9)
return this.add(1, b, c, 1, 0, 0)
}
/**
* Scales the matrix
* x (number) amount to be scaled, with `1` resulting in no change
* y (number) #optional amount to scale along the vertical axis. (Otherwise `x` applies to both axes.)
* cx (number) #optional horizontal origin point from which to scale
* cy (number) #optional vertical origin point from which to scale
* Default cx, cy is the middle point of the element.
*/
scale(x, y, cx, cy) {
y == null && (y = x) y == null && (y = x)
;(cx || cy) && this.translate(cx, cy) ;(cx || cy) && this.translate(cx, cy)
this.a *= x this.a *= x
@ -165,89 +177,27 @@ function Matrix(a, b, c, d, e, f) {
;(cx || cy) && this.translate(-cx, -cy) ;(cx || cy) && this.translate(-cx, -cy)
return this return this
} }
/*\
* Matrix.rotate /**
[ method ] * Returns x coordinate for given point after transformation described by the matrix. See also @Matrix.y
** * x (number)
* Rotates the matrix * y (number)
- a (number) angle of rotation, in degrees */
- x (number) horizontal origin point from which to rotate x(x, y) {
- y (number) vertical origin point from which to rotate
\*/
matrixproto.rotate = function (a, x, y) {
a = Snap.rad(a)
x = x || 0
y = y || 0
let cos = +math.cos(a).toFixed(9),
sin = +math.sin(a).toFixed(9)
this.add(cos, sin, -sin, cos, x, y)
return this.add(1, 0, 0, 1, -x, -y)
}
/*\
* Matrix.skewX
[ method ]
**
* Skews the matrix along the x-axis
- x (number) Angle to skew along the x-axis (in degrees).
\*/
matrixproto.skewX = function (x) {
return this.skew(x, 0)
}
/*\
* Matrix.skewY
[ method ]
**
* Skews the matrix along the y-axis
- y (number) Angle to skew along the y-axis (in degrees).
\*/
matrixproto.skewY = function (y) {
return this.skew(0, y)
}
/*\
* Matrix.skew
[ method ]
**
* Skews the matrix
- y (number) Angle to skew along the y-axis (in degrees).
- x (number) Angle to skew along the x-axis (in degrees).
\*/
matrixproto.skew = function (x, y) {
x = x || 0
y = y || 0
x = Snap.rad(x)
y = Snap.rad(y)
let c = math.tan(x).toFixed(9)
let b = math.tan(y).toFixed(9)
return this.add(1, b, c, 1, 0, 0)
}
/*\
* Matrix.x
[ method ]
**
* Returns x coordinate for given point after transformation described by the matrix. See also @Matrix.y
- x (number)
- y (number)
= (number) x
\*/
matrixproto.x = function (x, y) {
return x * this.a + y * this.c + this.e return x * this.a + y * this.c + this.e
} }
/*\ /*
* Matrix.y * Returns y coordinate for given point after transformation described by the matrix. See also @Matrix.x
[ method ] * x (number)
** * y (number)
* Returns y coordinate for given point after transformation described by the matrix. See also @Matrix.x */
- x (number) y(x, y) {
- y (number)
= (number) y
\*/
matrixproto.y = function (x, y) {
return x * this.b + y * this.d + this.f return x * this.b + y * this.d + this.f
} }
matrixproto.get = function (i) { get(i) {
return +this[Str.fromCharCode(97 + i)].toFixed(4) return +this[String.fromCharCode(97 + i)].toFixed(4)
} }
matrixproto.toString = function () { toString() {
return ( return (
'matrix(' + 'matrix(' +
[ [
@ -261,42 +211,29 @@ function Matrix(a, b, c, d, e, f) {
')' ')'
) )
} }
matrixproto.offset = function () {
offset() {
return [this.e.toFixed(4), this.f.toFixed(4)] return [this.e.toFixed(4), this.f.toFixed(4)]
} }
function norm(a) {
return a[0] * a[0] + a[1] * a[1] /**
} * Finds determinant of the given matrix.
function normalize(a) { */
let mag = math.sqrt(norm(a)) determinant() {
a[0] && (a[0] /= mag)
a[1] && (a[1] /= mag)
}
/*\
* Matrix.determinant
[ method ]
**
* Finds determinant of the given matrix.
= (number) determinant
\*/
matrixproto.determinant = function () {
return this.a * this.d - this.b * this.c return this.a * this.d - this.b * this.c
} }
/*\ /**
* Matrix.split * Splits matrix into primitive transformations
[ method ] = (object) in format:
** o dx (number) translation by x
* Splits matrix into primitive transformations o dy (number) translation by y
= (object) in format: o scalex (number) scale by x
o dx (number) translation by x o scaley (number) scale by y
o dy (number) translation by y o shear (number) shear
o scalex (number) scale by x o rotate (number) rotation in deg
o scaley (number) scale by y o isSimple (boolean) could it be represented via simple transformations
o shear (number) shear */
o rotate (number) rotation in deg split() {
o isSimple (boolean) could it be represented via simple transformations
\*/
matrixproto.split = function () {
let out = {} let out = {}
// translation // translation
out.dx = this.e out.dx = this.e
@ -307,7 +244,7 @@ function Matrix(a, b, c, d, e, f) {
[this.a, this.b], [this.a, this.b],
[this.c, this.d] [this.c, this.d]
] ]
out.scalex = math.sqrt(norm(row[0])) out.scalex = Math.sqrt(norm(row[0]))
normalize(row[0]) normalize(row[0])
out.shear = row[0][0] * row[1][0] + row[0][1] * row[1][1] out.shear = row[0][0] * row[1][0] + row[0][1] * row[1][1]
@ -316,7 +253,7 @@ function Matrix(a, b, c, d, e, f) {
row[1][1] - row[0][1] * out.shear row[1][1] - row[0][1] * out.shear
] ]
out.scaley = math.sqrt(norm(row[1])) out.scaley = Math.sqrt(norm(row[1]))
normalize(row[1]) normalize(row[1])
out.shear /= out.scaley out.shear /= out.scaley
@ -328,12 +265,12 @@ function Matrix(a, b, c, d, e, f) {
let sin = row[0][1], let sin = row[0][1],
cos = row[1][1] cos = row[1][1]
if (cos < 0) { if (cos < 0) {
out.rotate = Snap.deg(math.acos(cos)) out.rotate = deg(Math.acos(cos))
if (sin < 0) { if (sin < 0) {
out.rotate = 360 - out.rotate out.rotate = 360 - out.rotate
} }
} else { } else {
out.rotate = Snap.deg(math.asin(sin)) out.rotate = deg(Math.asin(sin))
} }
out.isSimple = out.isSimple =
@ -346,23 +283,19 @@ function Matrix(a, b, c, d, e, f) {
out.noRotation = !+out.shear.toFixed(9) && !out.rotate out.noRotation = !+out.shear.toFixed(9) && !out.rotate
return out return out
} }
/*\ /**
* Matrix.toTransformString * Returns transform string that represents given matrix
[ method ] */
** toTransformString(shorter) {
* Returns transform string that represents given matrix
= (string) transform string
\*/
matrixproto.toTransformString = function (shorter) {
let s = shorter || this.split() let s = shorter || this.split()
if (!+s.shear.toFixed(9)) { if (!+s.shear.toFixed(9)) {
s.scalex = +s.scalex.toFixed(4) s.scalex = +s.scalex.toFixed(4)
s.scaley = +s.scaley.toFixed(4) s.scaley = +s.scaley.toFixed(4)
s.rotate = +s.rotate.toFixed(4) s.rotate = +s.rotate.toFixed(4)
return ( return (
(s.dx || s.dy ? 't' + [+s.dx.toFixed(4), +s.dy.toFixed(4)] : E) + (s.dx || s.dy ? 't' + [+s.dx.toFixed(4), +s.dy.toFixed(4)] : '') +
(s.rotate ? 'r' + [+s.rotate.toFixed(4), 0, 0] : E) + (s.rotate ? 'r' + [+s.rotate.toFixed(4), 0, 0] : '') +
(s.scalex != 1 || s.scaley != 1 ? 's' + [s.scalex, s.scaley, 0, 0] : E) (s.scalex != 1 || s.scaley != 1 ? 's' + [s.scalex, s.scaley, 0, 0] : '')
) )
} else { } else {
return ( return (
@ -378,32 +311,4 @@ function Matrix(a, b, c, d, e, f) {
) )
} }
} }
})(Matrix.prototype)
/*\
* Snap.Matrix
[ method ]
**
* Matrix constructor, extend on your own risk.
* To create matrices use @Snap.matrix.
\*/
Snap.Matrix = Matrix
/*\
* Snap.matrix
[ method ]
**
* Utility method
**
* Returns a matrix based on the given parameters
- a (number)
- b (number)
- c (number)
- d (number)
- e (number)
- f (number)
* or
- svgMatrix (SVGMatrix)
= (object) @Matrix
\*/
Snap.matrix = function (a, b, c, d, e, f) {
return new Matrix(a, b, c, d, e, f)
} }

View File

@ -1,18 +1,11 @@
// Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. /**
// * {}
// Licensed under the Apache License, Version 2.0 (the "License"); * @author yutent<yutent.io@gmail.com>
// you may not use this file except in compliance with the License. * @date 2024/03/07 15:22:51
// You may obtain a copy of the License at */
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import eve from './eve.js' import eve from './eve.js'
import { Snap, SnapElement, Paper, Fragment, make, wrap } from './svg.js' import { Snap, SnapElement, Paper, make } from './svg.js'
import { $, is, uuid, preload } from './utils.js' import { $, is, uuid, preload } from './utils.js'
import { doc, win } from './lib/constants.js' import { doc, win } from './lib/constants.js'

View File

@ -1,18 +1,11 @@
// Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. /**
// * {}
// Licensed under the Apache License, Version 2.0 (the "License"); * @author yutent<yutent.io@gmail.com>
// you may not use this file except in compliance with the License. * @date 2024/03/07 15:33:05
// You may obtain a copy of the License at */
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import eve from './eve.js' import eve from './eve.js'
import { Snap, SnapElement, Paper, Fragment } from './svg.js' import { Animation } from './animation.js'
import mina from './mina.js' import mina from './mina.js'
import { is } from './utils.js' import { is } from './utils.js'
@ -85,33 +78,30 @@ setproto.forEach = function (callback, thisArg) {
} }
return this return this
} }
/*\ /**
* Set.animate * Animates each element in set in sync.
[ method ] *
** **
* Animates each element in set in sync. - attrs (object) key-value pairs of destination attributes
* - duration (number) duration of the animation in milliseconds
** - easing (function) #optional easing function from @mina or custom
- attrs (object) key-value pairs of destination attributes - callback (function) #optional callback function that executes when the animation ends
- duration (number) duration of the animation in milliseconds * or
- easing (function) #optional easing function from @mina or custom - animation (array) array of animation parameter for each element in set in format `[attrs, duration, easing, callback]`
- callback (function) #optional callback function that executes when the animation ends > Usage
* or | // animate all elements in set to radius 10
- animation (array) array of animation parameter for each element in set in format `[attrs, duration, easing, callback]` | set.animate({r: 10}, 500, mina.easein);
> Usage | // or
| // animate all elements in set to radius 10 | // animate first element to radius 10, but second to radius 20 and in different time
| set.animate({r: 10}, 500, mina.easein); | set.animate([{r: 10}, 500, mina.easein], [{r: 20}, 1500, mina.easein]);
| // or = (SnapElement) the current element
| // animate first element to radius 10, but second to radius 20 and in different time */
| set.animate([{r: 10}, 500, mina.easein], [{r: 20}, 1500, mina.easein]);
= (SnapElement) the current element
\*/
setproto.animate = function (attrs, ms, easing, callback) { setproto.animate = function (attrs, ms, easing, callback) {
if (typeof easing == 'function' && !easing.length) { if (typeof easing == 'function' && !easing.length) {
callback = easing callback = easing
easing = mina.linear easing = mina.linear
} }
if (attrs instanceof Snap._.Animation) { if (attrs instanceof Animation) {
callback = attrs.callback callback = attrs.callback
easing = attrs.easing easing = attrs.easing
ms = easing.dur ms = easing.dur
@ -147,24 +137,17 @@ setproto.animate = function (attrs, ms, easing, callback) {
} }
}) })
} }
/*\ /**
* Set.remove * Removes all children of the set.
[ method ] *
** */
* Removes all children of the set.
*
= (object) Set object
\*/
setproto.remove = function () { setproto.remove = function () {
while (this.length) { while (this.length) {
this.pop().remove() this.pop().remove()
} }
return this return this
} }
/*\ /**
* Set.bind
[ method ]
**
* Specifies how to handle a specific attribute when applied * Specifies how to handle a specific attribute when applied
* to a set. * to a set.
* *
@ -179,7 +162,7 @@ setproto.remove = function () {
- element (SnapElement) specific element in the set to apply the attribute to - element (SnapElement) specific element in the set to apply the attribute to
- eattr (string) attribute on the element to bind the attribute to - eattr (string) attribute on the element to bind the attribute to
= (object) Set object = (object) Set object
\*/ */
setproto.bind = function (attr, a, b) { setproto.bind = function (attr, a, b) {
let data = {} let data = {}
if (typeof a == 'function') { if (typeof a == 'function') {
@ -193,13 +176,9 @@ setproto.bind = function (attr, a, b) {
} }
return this return this
} }
/*\ /**
* Set.attr * Equivalent of @SnapElement.attr.
[ method ] */
**
* Equivalent of @SnapElement.attr.
= (object) Set object
\*/
setproto.attr = function (value) { setproto.attr = function (value) {
let unbound = {} let unbound = {}
for (let k in value) { for (let k in value) {
@ -214,28 +193,21 @@ setproto.attr = function (value) {
} }
return this return this
} }
/*\ /**
* Set.clear * Removes all elements from the set
[ method ] */
**
* Removes all elements from the set
\*/
setproto.clear = function () { setproto.clear = function () {
while (this.length) { while (this.length) {
this.pop() this.pop()
} }
} }
/*\ /**
* Set.splice * Removes range of elements from the set
[ method ] **
** * index (number) position of the deletion
* Removes range of elements from the set * count (number) number of element to remove
** * insertion (object) #optional elements to insert
- index (number) position of the deletion */
- count (number) number of element to remove
- insertion (object) #optional elements to insert
= (object) set elements that were deleted
\*/
setproto.splice = function (index, count, insertion) { setproto.splice = function (index, count, insertion) {
index = index < 0 ? mmax(this.length + index, 0) : index index = index < 0 ? mmax(this.length + index, 0) : index
count = mmax(0, mmin(this.length - index, count)) count = mmax(0, mmin(this.length - index, count))
@ -263,15 +235,11 @@ setproto.splice = function (index, count, insertion) {
} }
return new Set(todel) return new Set(todel)
} }
/*\ /**
* Set.exclude * Removes given element from the set
[ method ] **
** * element (object) element to remove
* Removes given element from the set */
**
- element (object) element to remove
= (boolean) `true` if object was found and removed from the set
\*/
setproto.exclude = function (el) { setproto.exclude = function (el) {
for (let i = 0, ii = this.length; i < ii; i++) for (let i = 0, ii = this.length; i < ii; i++)
if (this[i] == el) { if (this[i] == el) {
@ -280,15 +248,11 @@ setproto.exclude = function (el) {
} }
return false return false
} }
/*\ /**
* Set.insertAfter * Inserts set elements after given element.
[ method ] **
** * element (object) set will be inserted after this element
* Inserts set elements after given element. */
**
- element (object) set will be inserted after this element
= (object) Set object
\*/
setproto.insertAfter = function (el) { setproto.insertAfter = function (el) {
let i = this.items.length let i = this.items.length
while (i--) { while (i--) {
@ -296,13 +260,9 @@ setproto.insertAfter = function (el) {
} }
return this return this
} }
/*\ /**
* Set.getBBox * Union of all bboxes of the set. See @SnapElement.getBBox.
[ method ] */
**
* Union of all bboxes of the set. See @SnapElement.getBBox.
= (object) bounding box descriptor. See @SnapElement.getBBox.
\*/
setproto.getBBox = function () { setproto.getBBox = function () {
let x = [], let x = [],
y = [], y = [],
@ -331,14 +291,9 @@ setproto.getBBox = function () {
cy: y + (y2 - y) / 2 cy: y + (y2 - y) / 2
} }
} }
/*\ /**
* Set.insertAfter * Creates a clone of the set.
[ method ] */
**
* Creates a clone of the set.
**
= (object) New Set object
\*/
setproto.clone = function (s) { setproto.clone = function (s) {
s = new Set() s = new Set()
for (let i = 0, ii = this.items.length; i < ii; i++) { for (let i = 0, ii = this.items.length; i < ii; i++) {
@ -346,33 +301,21 @@ setproto.clone = function (s) {
} }
return s return s
} }
setproto.toString = function () {
return 'Snap\u2018s set'
}
setproto.type = 'set' setproto.type = 'set'
// export
/*\ /**
* Snap.Set * Creates a set and fills it with list of arguments.
[ property ] **
** = (object) New Set object
* Set constructor. | let r = paper.rect(0, 0, 10, 10),
\*/ | s1 = createSet(), // empty set
Snap.Set = Set | s2 = createSet(r, paper.circle(100, 100, 20)); // prefilled set
/*\ */
* Snap.set export function createSet(...args) {
[ method ]
**
* Creates a set and fills it with list of arguments.
**
= (object) New Set object
| let r = paper.rect(0, 0, 10, 10),
| s1 = Snap.set(), // empty set
| s2 = Snap.set(r, paper.circle(100, 100, 20)); // prefilled set
\*/
Snap.set = function () {
let set = new Set() let set = new Set()
if (arguments.length) { if (args.length) {
set.push.apply(set, Array.prototype.slice.call(arguments, 0)) set.push.apply(set, args)
} }
return set return set
} }

View File

@ -22,6 +22,8 @@ import {
PATH_VALUES PATH_VALUES
} from './lib/constants.js' } from './lib/constants.js'
import { Matrix } from './matrix.js'
import { import {
$, $,
is, is,
@ -276,7 +278,7 @@ export class Snap {
\*/ \*/
static selectAll(query = '') { static selectAll(query = '') {
let nodelist = doc.querySelectorAll(query), let nodelist = doc.querySelectorAll(query),
set = (Snap.set || Array)() set = []
for (let i = 0; i < nodelist.length; i++) { for (let i = 0; i < nodelist.length; i++) {
set.push(wrap(nodelist[i])) set.push(wrap(nodelist[i]))
} }
@ -309,9 +311,7 @@ export class Snap {
if (w.nodeType) { if (w.nodeType) {
return wrap(w) return wrap(w)
} }
if (is(w, 'array') && Snap.set) {
return Snap.set.apply(Snap, w)
}
if (w instanceof SnapElement) { if (w instanceof SnapElement) {
return w return w
} }
@ -465,7 +465,7 @@ Snap._.rgTransform = /^[a-z][\s]*-?\.?\d/i
function transform2matrix(tstr, bbox) { function transform2matrix(tstr, bbox) {
let tdata = parseTransformString(tstr), let tdata = parseTransformString(tstr),
m = new Snap.Matrix() m = new Matrix()
if (tdata) { if (tdata) {
for (let i = 0, ii = tdata.length; i < ii; i++) { for (let i = 0, ii = tdata.length; i < ii; i++) {
let t = tdata[i], let t = tdata[i],
@ -1344,6 +1344,10 @@ export class SnapElement {
return out[0] return out[0]
} }
equal(name, b) {
return eve('snap.util.equal', this, name, b).firstDefined()
}
addClass(value) { addClass(value) {
this.node.classList.add(value) this.node.classList.add(value)
return this return this