Visualize Your Ideas With Code
feat: AnimationClip
aarthificial681146a8e92a
diff --git a/src/Scene.ts b/src/Scene.tsindex 3f740054..caa269c0 100644--- a/src/Scene.ts+++ b/src/Scene.ts@@ -52,6 +52,7 @@ export class Scene extends Layer {public reload(runnerFactory: SceneRunner) {this.runnerFactory = runnerFactory;+ this.debug(null);this.lastFrame = null;}diff --git a/src/components/AnimationClip.ts b/src/components/AnimationClip.tsnew file mode 100644index 00000000..c3c94bbe--- /dev/null+++ b/src/components/AnimationClip.ts@@ -0,0 +1,93 @@+import {getset, KonvaNode} from '../decorators';+import {Sprite, SpriteData} from './Sprite';+import {GetSet} from 'konva/lib/types';+import {LinearLayout} from './LinearLayout';+import {Center} from '../types';+import {Surface, SurfaceConfig} from './Surface';+import {getStyle, Style} from '../styles';++export interface AnimationClipConfig extends SurfaceConfig {+ animation: SpriteData[];+ skin?: SpriteData;+ frame?: number;+ style?: Partial<Style>;+}++@KonvaNode()+export class AnimationClip extends Surface {+ @getset(null, AnimationClip.prototype.update)+ public animation: GetSet<AnimationClipConfig['animation'], this>;++ @getset(null, AnimationClip.prototype.update)+ public skin: GetSet<AnimationClipConfig['skin'], this>;++ @getset(0, AnimationClip.prototype.updateStyle)+ public frame: GetSet<AnimationClipConfig['frame'], this>;++ @getset(null, AnimationClip.prototype.updateStyle)+ public style: GetSet<AnimationClipConfig['style'], this>;++ public constructor(config?: AnimationClipConfig) {+ super({+ direction: Center.Horizontal,+ ...config,+ });+ this.setChild(new LinearLayout({direction: Center.Horizontal, padd: 5}));+ this.update();+ }++ private update() {+ const animation = this.animation();+ const layout = this.getChild<LinearLayout>();+ if (!animation || !layout) return;++ const skin = this.skin();+ for (let i = 0; i < animation.length; i++) {+ let sprite: Sprite;+ let surface: Surface;+ if (layout.children.length > i) {+ surface = <Surface>layout.children[i];+ sprite = surface.getChild<Sprite>();+ sprite.animation(animation);+ sprite.skin(skin);+ } else {+ surface = new Surface({+ padd: 22,+ margin: 5,+ });+ sprite = new Sprite({+ animation,+ skin,+ width: 96,+ height: 96,+ });+ surface.setChild(sprite);+ layout.add(surface);+ }+ }+ for (let i = layout.children.length - 1; i >= animation.length; i--) {+ layout.children[i].destroy();+ }+ this.updateStyle();+ }++ private updateStyle() {+ const style = getStyle(this);+ const layout = this.getChild<LinearLayout>();+ const animation = this.animation();+ if (!animation || !layout) return;++ const children = this.getChild<LinearLayout>().children;+ const frame = this.frame() % animation.length;++ this.background(style.background);++ for (let i = 0; i < children.length; i++) {+ const surface = <Surface>children[i];+ surface.background(+ frame === i ? style.backgroundLight : 'rgba(0, 0, 0, 0)',+ );+ surface.getChild<Sprite>().frame(i);+ }+ }+}diff --git a/src/components/LayoutGroup.ts b/src/components/LayoutGroup.tsindex 7806f299..476d36e8 100644--- a/src/components/LayoutGroup.ts+++ b/src/components/LayoutGroup.ts@@ -95,5 +95,10 @@ export abstract class LayoutGroup extends Group implements ILayoutNode {this.getParent()?.fire(LAYOUT_CHANGE_EVENT, undefined, true);}+ public _setChildrenIndices() {+ super._setChildrenIndices();+ this.handleLayoutChange();+ }+protected handleLayoutChange() {}}diff --git a/src/components/LinearLayout.ts b/src/components/LinearLayout.tsindex 046d9258..a18f8268 100644--- a/src/components/LinearLayout.ts+++ b/src/components/LinearLayout.ts@@ -4,8 +4,8 @@ import {Shape} from 'konva/lib/Shape';import {LayoutGroup, LayoutGroupConfig} from './LayoutGroup';import {Center, Origin, Size, Spacing} from '../types';import {Container} from 'konva/lib/Container';-import {getClientRect, getOriginDelta, isLayoutNode} from "./ILayoutNode";-import {getset, KonvaNode} from "../decorators";+import {getClientRect, getOriginDelta, isLayoutNode} from './ILayoutNode';+import {getset, KonvaNode} from '../decorators';export interface LinearLayoutConfig extends LayoutGroupConfig {direction?: Center;@@ -39,6 +39,7 @@ export class LinearLayout extends LayoutGroup {protected handleLayoutChange() {if (!this.children) return;+ const direction = this.direction();this.contentSize = {width: 0, height: 0};for (const child of this.children) {@@ -46,26 +47,51 @@ export class LinearLayout extends LayoutGroup {const size = isLayout ? child.getLayoutSize() : child.getSize();const margin = isLayout ? child.getMargin() : new Spacing();const scale = child.getAbsoluteScale(this);- this.contentSize.width = Math.max(- this.contentSize.width,- (size.width + margin.x) * scale.x,- );- this.contentSize.height += (size.height + margin.y) * scale.y;++ const boxSize = {+ x: (size.width + margin.x) * scale.x,+ y: (size.height + margin.y) * scale.y,+ };++ if (direction === Center.Vertical) {+ this.contentSize.width = Math.max(this.contentSize.width, boxSize.x);+ this.contentSize.height += boxSize.y;+ } else {+ this.contentSize.height = Math.max(this.contentSize.height, boxSize.y);+ this.contentSize.width += boxSize.x;+ }}- let height = this.contentSize.height / -2;+ let length =+ direction === Center.Vertical+ ? this.contentSize.height / -2+ : this.contentSize.width / -2;+for (const child of this.children) {const isLayout = isLayoutNode(child);const size = isLayout ? child.getLayoutSize() : child.getSize();const margin = isLayout ? child.getMargin() : new Spacing();- const offset = isLayout ? child.getOriginDelta(Origin.Top) : getOriginDelta(size, Origin.TopLeft, Origin.Top);const scale = child.getAbsoluteScale(this);- child.position({- x: -offset.x * scale.x,- y: height + (-offset.y + margin.top) * scale.y,- });- height += (size.height + margin.y) * scale.y;+ if (direction === Center.Vertical) {+ const offset = isLayout+ ? child.getOriginDelta(Origin.Top)+ : getOriginDelta(size, Origin.TopLeft, Origin.Top);+ child.position({+ x: -offset.x * scale.x,+ y: length + (-offset.y + margin.top) * scale.y,+ });+ length += (size.height + margin.y) * scale.y;+ } else {+ const offset = isLayout+ ? child.getOriginDelta(Origin.Left)+ : getOriginDelta(size, Origin.TopLeft, Origin.Left);+ child.position({+ x: length + (-offset.x + margin.left) * scale.x,+ y: -offset.y * scale.y,+ });+ length += (size.width + margin.x) * scale.x;+ }}this.offset(this.getOriginOffset());diff --git a/src/components/Pin.ts b/src/components/Pin.tsindex b30b4bcd..9276edd2 100644--- a/src/components/Pin.ts+++ b/src/components/Pin.ts@@ -19,9 +19,9 @@ export const PIN_CHANGE_EVENT = 'pinChange';export class Pin extends Group {@getset(null, Pin.prototype.firePinChangeEvent)public target: GetSet<PinConfig['target'], this>;- @getset(null)+ @getset(null, Pin.prototype.updateAttach)public attach: GetSet<PinConfig['attach'], this>;- @getset()+ @getset(null, Pin.prototype.firePinChangeEvent)public direction: GetSet<PinConfig['direction'], this>;public constructor(config?: PinConfig) {@@ -55,6 +55,11 @@ export class Pin extends Group {@bind()private firePinChangeEvent() {+ this.updateAttach();+ this.fire(PIN_CHANGE_EVENT, undefined, true);+ }++ private updateAttach() {const attach = this.attach();if (attach) {const attachDirection = flipOrigin(attach.getOrigin(), this.direction());@@ -65,8 +70,6 @@ export class Pin extends Group {y: rect.y + offset.y,});}-- this.fire(PIN_CHANGE_EVENT, undefined, true);}public getClientRect(config?: {diff --git a/src/components/Sprite.ts b/src/components/Sprite.tsindex 515c0510..11eacaa3 100644--- a/src/components/Sprite.ts+++ b/src/components/Sprite.ts@@ -22,11 +22,12 @@ export interface SpriteConfig extends LayoutShapeConfig {playing?: boolean;fps?: number;maskBlend?: number;+ frame?: number;}export const SPRITE_CHANGE_EVENT = 'spriteChange';-const COMPUTE_CANVAS_SIZE = 1024;+const COMPUTE_CANVAS_SIZE = 64;@KonvaNode()export class Sprite extends LayoutShape {@@ -42,14 +43,15 @@ export class Sprite extends LayoutShape {public fps: GetSet<SpriteConfig['fps'], this>;@getset(0, Sprite.prototype.recalculate)public maskBlend: GetSet<SpriteConfig['maskBlend'], this>;+ @getset(0, Sprite.prototype.recalculate)+ public frame: GetSet<SpriteConfig['frame'], this>;- private frame: SpriteData = {+ private spriteData: SpriteData = {height: 0,width: 0,data: [],fileName: '',};- private frameId: number = 0;private task: ThreadGenerator | null = null;private imageData: ImageData;private readonly computeCanvas: HTMLCanvasElement;@@ -74,8 +76,8 @@ export class Sprite extends LayoutShape {this.computeCanvas,0,0,- this.frame.width,- this.frame.height,+ this.spriteData.width,+ this.spriteData.height,size.width / -2,size.height / -2,size.width,@@ -91,28 +93,28 @@ export class Sprite extends LayoutShape {const blend = this.maskBlend();if (!this.context || !animation || animation.length === 0) return;- this.frameId %= animation.length;- this.frame = animation[this.frameId];+ const frameId = this.frame() % animation.length;+ this.spriteData = animation[frameId];this.offset(this.getOriginOffset());this.imageData = this.context.createImageData(- this.frame.width,- this.frame.height,+ this.spriteData.width,+ this.spriteData.height,);if (skin) {- for (let y = 0; y < this.frame.height; y++) {- for (let x = 0; x < this.frame.width; x++) {+ for (let y = 0; y < this.spriteData.height; y++) {+ for (let x = 0; x < this.spriteData.width; x++) {const id = this.positionToId({x, y});- const skinX = this.frame.data[id];- const skinY = this.frame.data[id + 1];+ const skinX = this.spriteData.data[id];+ const skinY = this.spriteData.data[id + 1];const skinId = ((skin.height - 1 - skinY) * skin.width + skinX) * 4;this.imageData.data[id] = skin.data[skinId];this.imageData.data[id + 1] = skin.data[skinId + 1];this.imageData.data[id + 2] = skin.data[skinId + 2];this.imageData.data[id + 3] = Math.round(- (this.frame.data[id + 3] / 255) *+ (this.spriteData.data[id + 3] / 255) *(skin.data[skinId + 3] / 255) *255,);@@ -123,7 +125,7 @@ export class Sprite extends LayoutShape {}}} else {- this.imageData.data.set(this.frame.data);+ this.imageData.data.set(this.spriteData.data);}this.context.putImageData(this.imageData, 0, 0);@@ -157,11 +159,10 @@ export class Sprite extends LayoutShape {@threadable()private *playRunner(): ThreadGenerator {- this.frameId = 0;+ this.frame(0);while (this.task !== null) {if (this.playing()) {- this.frameId++;- this.recalculate();+ this.frame(this.frame() + 1);}yield* waitFor(1 / this.fps());}@@ -170,7 +171,7 @@ export class Sprite extends LayoutShape {@threadable()public *waitForFrame(frame: number): ThreadGenerator {let limit = 1000;- while (this.frameId !== frame && limit > 0) {+ while (this.frame() !== frame && limit > 0) {limit--;yield;}diff --git a/src/patches/Factory.ts b/src/patches/Factory.tsindex 428c27dc..ca55db67 100644--- a/src/patches/Factory.ts+++ b/src/patches/Factory.ts@@ -30,8 +30,9 @@ Factory.overWriteSetter = function overWriteSetter(return new Animator(this, attr).key(val, time).run();}+ const old = this.attrs[attr];this._setAttr(attr, val);- after?.call(this);+ if (old !== val) after?.call(this);return this;};