/** * {} * @author yutent * @date 2024/03/07 15:17:00 */ import eve from './eve.js' let operators = { '+': function (x, y) { return x + y }, '-': function (x, y) { return x - y }, '/': function (x, y) { return x / y }, '*': function (x, y) { return x * y } }, reUnit = /[a-z]+$/i, reAddon = /^\s*([+\-\/*])\s*=\s*([\d.eE+\-]+)\s*([^\d\s]+)?\s*$/ function getUnit(unit) { return function (val) { return +val.toFixed(3) + unit } } eve.on('snap.util.attr', function (val) { let plus = String(val).match(reAddon) if (plus) { let evnt = eve.nt(), name = evnt.substring(evnt.lastIndexOf('.') + 1), a = this.attr(name), atr = {} eve.stop() let unit = plus[3] || '', aUnit = a.match(reUnit), op = operators[plus[1]] if (aUnit && aUnit == unit) { val = op(+a, +plus[2]) } else { a = this.asPX(name) val = op(this.asPX(name), this.asPX(name, plus[2] + unit)) } if (isNaN(a) || isNaN(val)) { return } atr[name] = val this.attr(atr) } })(-10) eve.on('snap.util.equal', function (name, b) { let a = String(this.attr(name) || '') let bplus = String(b).match(reAddon) if (bplus) { eve.stop() let unit = bplus[3] || '', aUnit = a.match(reUnit), op = operators[bplus[1]] if (aUnit && aUnit === unit) { return { from: +a, to: op(+a, +bplus[2]), f: getUnit(aUnit) } } else { a = this.asPX(name) return { from: a, to: op(a, this.asPX(name, bplus[2] + unit)), f: _ => _ } } } })(-10)