提交 0f49ca95 编写于 作者: fxy060608's avatar fxy060608

chore: merge

...@@ -352,9 +352,11 @@ const defaultState = { ...@@ -352,9 +352,11 @@ const defaultState = {
fontFamily: 'sans-serif', fontFamily: 'sans-serif',
} }
type ActionsItemType = string | number | boolean | undefined | Array<number>
type ActionsItemData = Array<ActionsItemType>
type ActionsItem = { type ActionsItem = {
method: string method: string
data: Array<string | number | boolean> data: ActionsItemData | Array<ActionsItem>
} }
type DefaultState = typeof defaultState type DefaultState = typeof defaultState
type Callback = (result: any) => void | undefined type Callback = (result: any) => void | undefined
...@@ -793,21 +795,20 @@ export class CanvasContext implements UniApp.CanvasContext { ...@@ -793,21 +795,20 @@ export class CanvasContext implements UniApp.CanvasContext {
;[...methods1, ...methods2].forEach(function (method) { ;[...methods1, ...methods2].forEach(function (method) {
function get(method: string) { function get(method: string) {
// @ts-ignore
let _this = this as CanvasContext
switch (method) { switch (method) {
case 'fill': case 'fill':
case 'stroke': case 'stroke':
return function () { return function () {
// @ts-ignore _this.actions.push({
this.actions.push({
method: method + 'Path', method: method + 'Path',
// @ts-ignore data: [..._this.path],
data: [...this.path],
}) })
} }
case 'fillRect': case 'fillRect':
return function (x: number, y: number, width: number, height: number) { return function (x: number, y: number, width: number, height: number) {
// @ts-ignore _this.actions.push({
this.actions.push({
method: 'fillPath', method: 'fillPath',
data: [ data: [
{ {
...@@ -819,8 +820,7 @@ export class CanvasContext implements UniApp.CanvasContext { ...@@ -819,8 +820,7 @@ export class CanvasContext implements UniApp.CanvasContext {
} }
case 'strokeRect': case 'strokeRect':
return function (x: number, y: number, width: number, height: number) { return function (x: number, y: number, width: number, height: number) {
// @ts-ignore _this.actions.push({
this.actions.push({
method: 'strokePath', method: 'strokePath',
data: [ data: [
{ {
...@@ -837,8 +837,7 @@ export class CanvasContext implements UniApp.CanvasContext { ...@@ -837,8 +837,7 @@ export class CanvasContext implements UniApp.CanvasContext {
if (typeof maxWidth === 'number') { if (typeof maxWidth === 'number') {
data.push(maxWidth) data.push(maxWidth)
} }
// @ts-ignore _this.actions.push({
this.actions.push({
method, method,
data, data,
}) })
...@@ -865,7 +864,7 @@ export class CanvasContext implements UniApp.CanvasContext { ...@@ -865,7 +864,7 @@ export class CanvasContext implements UniApp.CanvasContext {
dWidth = undefined dWidth = undefined
dHeight = undefined dHeight = undefined
} }
var data let data: ActionsItemData
function isNumber(e: any) { function isNumber(e: any) {
return typeof e === 'number' return typeof e === 'number'
...@@ -889,16 +888,14 @@ export class CanvasContext implements UniApp.CanvasContext { ...@@ -889,16 +888,14 @@ export class CanvasContext implements UniApp.CanvasContext {
: isNumber(sWidth) && isNumber(sHeight) : isNumber(sWidth) && isNumber(sHeight)
? [imageResource, sx, sy, sWidth, sHeight] ? [imageResource, sx, sy, sWidth, sHeight]
: [imageResource, sx, sy] : [imageResource, sx, sy]
// @ts-ignore _this.actions.push({
this.actions.push({
method, method,
data, data,
}) })
} }
default: default:
return function (...data: any) { return function (...data: ActionsItemData) {
// @ts-ignore _this.actions.push({
this.actions.push({
method, method,
data, data,
}) })
...@@ -909,29 +906,32 @@ export class CanvasContext implements UniApp.CanvasContext { ...@@ -909,29 +906,32 @@ export class CanvasContext implements UniApp.CanvasContext {
}) })
methods3.forEach(function (method) { methods3.forEach(function (method) {
function get(method: string) { function get(method: string) {
// @ts-ignore
let _this = this as CanvasContext
switch (method) { switch (method) {
case 'setFillStyle': case 'setFillStyle':
case 'setStrokeStyle': case 'setStrokeStyle':
return function (color: string | Data) { return function (color: string | Data) {
if (typeof color !== 'object') { if (typeof color !== 'object') {
// @ts-ignore _this.actions.push({
this.actions.push({
method, method,
data: ['normal', checkColor(color)], data: ['normal', checkColor(color)],
}) })
} else { } else {
// @ts-ignore _this.actions.push({
this.actions.push({
method, method,
data: [color.type, color.data, color.colorStop], data: [
color.type as ActionsItemType,
color.data as ActionsItemType,
color.colorStop as ActionsItemType,
],
}) })
} }
} }
case 'setGlobalAlpha': case 'setGlobalAlpha':
return function (alpha: number) { return function (alpha: number) {
alpha = Math.floor(255 * parseFloat(alpha as unknown as string)) alpha = Math.floor(255 * parseFloat(alpha as unknown as string))
// @ts-ignore _this.actions.push({
this.actions.push({
method, method,
data: [alpha], data: [alpha],
}) })
...@@ -943,52 +943,41 @@ methods3.forEach(function (method) { ...@@ -943,52 +943,41 @@ methods3.forEach(function (method) {
blur: number, blur: number,
color: string color: string
) { ) {
color = checkColor(color) as any let _color = checkColor(color)
// @ts-ignore _this.actions.push({
this.actions.push({
method, method,
data: [offsetX, offsetY, blur, color], data: [offsetX, offsetY, blur, color],
}) })
// @ts-ignore _this.state.shadowBlur = blur
this.state.shadowBlur = blur _this.state.shadowColor = _color
// @ts-ignore _this.state.shadowOffsetX = offsetX
this.state.shadowColor = color _this.state.shadowOffsetY = offsetY
// @ts-ignore
this.state.shadowOffsetX = offsetX
// @ts-ignore
this.state.shadowOffsetY = offsetY
} }
case 'setLineDash': case 'setLineDash':
return function (pattern: Array<number> | undefined, offset: number) { return function (pattern: Array<number> | undefined, offset: number) {
pattern = pattern || [0, 0] pattern = pattern || [0, 0]
offset = offset || 0 offset = offset || 0
// @ts-ignore _this.actions.push({
this.actions.push({
method, method,
data: [pattern, offset], data: [pattern, offset],
}) })
// @ts-ignore _this.state.lineDash = pattern
this.state.lineDash = pattern
} }
case 'setFontSize': case 'setFontSize':
return function (fontSize: number) { return function (fontSize: number) {
// @ts-ignore _this.state.font = _this.state.font.replace(
this.state.font = this.state.font.replace(
/\d+\.?\d*px/, /\d+\.?\d*px/,
fontSize + 'px' fontSize + 'px'
) )
// @ts-ignore _this.state.fontSize = fontSize
this.state.fontSize = fontSize _this.actions.push({
// @ts-ignore
this.actions.push({
method, method,
data: [fontSize], data: [fontSize],
}) })
} }
default: default:
return function (...data: any) { return function (...data: ActionsItemData) {
// @ts-ignore _this.actions.push({
this.actions.push({
method, method,
data, data,
}) })
......
const defaultOption: Option = {
duration: 400,
timingFunction: 'linear',
delay: 0,
transformOrigin: '50% 50% 0',
}
type Option = UniApp.CreateAnimationOptions
type AnimatesArgs = string[]
type Action = {
animates: string[]
option: ReturnType<MPAnimation['_getOption']>
}
class MPAnimation implements UniApp.Animation {
actions: Array<Action>
currentTransform: Data
currentStepAnimates: Array<{ type: string; args: AnimatesArgs }>
option: Option
constructor(option: Option) {
this.actions = []
this.currentTransform = {}
this.currentStepAnimates = []
this.option = Object.assign({}, defaultOption, option)
}
_getOption(option: Option) {
const _option: {
transition: Option
transformOrigin: Option['transformOrigin']
} = {
transition: Object.assign({}, this.option, option),
transformOrigin: '',
}
_option.transformOrigin = _option.transition.transformOrigin
delete _option.transition.transformOrigin
return _option
}
_pushAnimates(type: string, args: AnimatesArgs) {
this.currentStepAnimates.push({
type: type,
args: args,
})
}
_converType(type: string) {
return type.replace(/[A-Z]/g, (text) => {
return `-${text.toLowerCase()}`
})
}
_getValue(value: string | number) {
return typeof value === 'number' ? `${value}px` : value
}
export() {
const actions = this.actions
this.actions = []
return {
actions,
}
}
step(option: Option) {
this.currentStepAnimates.forEach((animate) => {
if (animate.type !== 'style') {
this.currentTransform[animate.type] = animate
} else {
this.currentTransform[`${animate.type}.${animate.args[0]}`] = animate
}
})
this.actions.push({
animates: Object.values(this.currentTransform) as Action['animates'],
option: this._getOption(option),
})
this.currentStepAnimates = []
return this
}
'matrix': UniApp.Animation['matrix']
'matrix3d': UniApp.Animation['matrix3d']
'rotate': UniApp.Animation['rotate']
'rotate3d': UniApp.Animation['rotate3d']
'rotateX': UniApp.Animation['rotateX']
'rotateY': UniApp.Animation['rotateY']
'rotateZ': UniApp.Animation['rotateZ']
'scale': UniApp.Animation['scale']
'scale3d': UniApp.Animation['scale3d']
'scaleX': UniApp.Animation['scaleX']
'scaleY': UniApp.Animation['scaleY']
'scaleZ': UniApp.Animation['scaleZ']
'skew': UniApp.Animation['skew']
'skewX': UniApp.Animation['skewX']
'skewY': UniApp.Animation['skewY']
'translate': UniApp.Animation['translate']
'translate3d': UniApp.Animation['translate3d']
'translateX': UniApp.Animation['translateX']
'translateY': UniApp.Animation['translateY']
'translateZ': UniApp.Animation['translateZ']
'opacity': UniApp.Animation['opacity']
'backgroundColor': UniApp.Animation['backgroundColor']
'width': UniApp.Animation['width']
'height': UniApp.Animation['height']
'right': UniApp.Animation['right']
'top': UniApp.Animation['top']
'bottom': UniApp.Animation['bottom']
'left': UniApp.Animation['left']
}
const animateTypes1 = [
'matrix',
'matrix3d',
'rotate',
'rotate3d',
'rotateX',
'rotateY',
'rotateZ',
'scale',
'scale3d',
'scaleX',
'scaleY',
'scaleZ',
'skew',
'skewX',
'skewY',
'translate',
'translate3d',
'translateX',
'translateY',
'translateZ',
]
const animateTypes2 = ['opacity', 'backgroundColor']
const animateTypes3 = ['width', 'height', 'left', 'right', 'top', 'bottom']
animateTypes1.concat(animateTypes2, animateTypes3).forEach((type) => {
;(MPAnimation.prototype as any)[type] = function (...args: AnimatesArgs) {
let _this = this as MPAnimation
if (animateTypes2.concat(animateTypes3).includes(type)) {
_this._pushAnimates('style', [
_this._converType(type),
animateTypes3.includes(type) ? _this._getValue(args[0]) : args[0],
])
} else {
_this._pushAnimates(type, args)
}
return this
}
})
export function createAnimation(option: Option) {
return new MPAnimation(option)
}
...@@ -2762,18 +2762,19 @@ class CanvasContext { ...@@ -2762,18 +2762,19 @@ class CanvasContext {
} }
[...methods1, ...methods2].forEach(function(method) { [...methods1, ...methods2].forEach(function(method) {
function get(method2) { function get(method2) {
let _this = this;
switch (method2) { switch (method2) {
case "fill": case "fill":
case "stroke": case "stroke":
return function() { return function() {
this.actions.push({ _this.actions.push({
method: method2 + "Path", method: method2 + "Path",
data: [...this.path] data: [..._this.path]
}); });
}; };
case "fillRect": case "fillRect":
return function(x, y, width, height) { return function(x, y, width, height) {
this.actions.push({ _this.actions.push({
method: "fillPath", method: "fillPath",
data: [ data: [
{ {
...@@ -2785,7 +2786,7 @@ class CanvasContext { ...@@ -2785,7 +2786,7 @@ class CanvasContext {
}; };
case "strokeRect": case "strokeRect":
return function(x, y, width, height) { return function(x, y, width, height) {
this.actions.push({ _this.actions.push({
method: "strokePath", method: "strokePath",
data: [ data: [
{ {
...@@ -2802,7 +2803,7 @@ class CanvasContext { ...@@ -2802,7 +2803,7 @@ class CanvasContext {
if (typeof maxWidth === "number") { if (typeof maxWidth === "number") {
data.push(maxWidth); data.push(maxWidth);
} }
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data data
}); });
...@@ -2819,7 +2820,7 @@ class CanvasContext { ...@@ -2819,7 +2820,7 @@ class CanvasContext {
dWidth = void 0; dWidth = void 0;
dHeight = void 0; dHeight = void 0;
} }
var data; let data;
function isNumber(e2) { function isNumber(e2) {
return typeof e2 === "number"; return typeof e2 === "number";
} }
...@@ -2834,14 +2835,14 @@ class CanvasContext { ...@@ -2834,14 +2835,14 @@ class CanvasContext {
dWidth, dWidth,
dHeight dHeight
] : isNumber(sWidth) && isNumber(sHeight) ? [imageResource, sx, sy, sWidth, sHeight] : [imageResource, sx, sy]; ] : isNumber(sWidth) && isNumber(sHeight) ? [imageResource, sx, sy, sWidth, sHeight] : [imageResource, sx, sy];
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data data
}); });
}; };
default: default:
return function(...data) { return function(...data) {
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data data
}); });
...@@ -2852,64 +2853,69 @@ class CanvasContext { ...@@ -2852,64 +2853,69 @@ class CanvasContext {
}); });
methods3.forEach(function(method) { methods3.forEach(function(method) {
function get(method2) { function get(method2) {
let _this = this;
switch (method2) { switch (method2) {
case "setFillStyle": case "setFillStyle":
case "setStrokeStyle": case "setStrokeStyle":
return function(color) { return function(color) {
if (typeof color !== "object") { if (typeof color !== "object") {
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data: ["normal", checkColor(color)] data: ["normal", checkColor(color)]
}); });
} else { } else {
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data: [color.type, color.data, color.colorStop] data: [
color.type,
color.data,
color.colorStop
]
}); });
} }
}; };
case "setGlobalAlpha": case "setGlobalAlpha":
return function(alpha) { return function(alpha) {
alpha = Math.floor(255 * parseFloat(alpha)); alpha = Math.floor(255 * parseFloat(alpha));
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data: [alpha] data: [alpha]
}); });
}; };
case "setShadow": case "setShadow":
return function(offsetX, offsetY, blur, color) { return function(offsetX, offsetY, blur, color) {
color = checkColor(color); let _color = checkColor(color);
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data: [offsetX, offsetY, blur, color] data: [offsetX, offsetY, blur, color]
}); });
this.state.shadowBlur = blur; _this.state.shadowBlur = blur;
this.state.shadowColor = color; _this.state.shadowColor = _color;
this.state.shadowOffsetX = offsetX; _this.state.shadowOffsetX = offsetX;
this.state.shadowOffsetY = offsetY; _this.state.shadowOffsetY = offsetY;
}; };
case "setLineDash": case "setLineDash":
return function(pattern, offset) { return function(pattern, offset) {
pattern = pattern || [0, 0]; pattern = pattern || [0, 0];
offset = offset || 0; offset = offset || 0;
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data: [pattern, offset] data: [pattern, offset]
}); });
this.state.lineDash = pattern; _this.state.lineDash = pattern;
}; };
case "setFontSize": case "setFontSize":
return function(fontSize) { return function(fontSize) {
this.state.font = this.state.font.replace(/\d+\.?\d*px/, fontSize + "px"); _this.state.font = _this.state.font.replace(/\d+\.?\d*px/, fontSize + "px");
this.state.fontSize = fontSize; _this.state.fontSize = fontSize;
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data: [fontSize] data: [fontSize]
}); });
}; };
default: default:
return function(...data) { return function(...data) {
this.actions.push({ _this.actions.push({
method: method2, method: method2,
data data
}); });
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册