From fcb685bc7f6946dd678cdac517e87dfd3567b7b3 Mon Sep 17 00:00:00 2001 From: kmkzt Date: Thu, 30 May 2019 01:11:22 +0900 Subject: [PATCH] feat: add load methods --- src/SvgAnimation.ts | 35 +++++++++++++++++++++++++++++++++++ src/example/index.tsx | 12 ++++-------- src/utils/svgFormatting.ts | 10 +++++++--- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/SvgAnimation.ts b/src/SvgAnimation.ts index a2d615354..b07845b34 100644 --- a/src/SvgAnimation.ts +++ b/src/SvgAnimation.ts @@ -1,4 +1,5 @@ import Two, { ConstructorParams } from 'two.js' +import { svgFormatting } from './utils/svgFormatting' export interface AnimationOption extends ConstructorParams { el: SvgAnimation['el'] shakingRange?: SvgAnimation['shakingRange'] @@ -16,6 +17,8 @@ export class SvgAnimation extends Two { this.shaking = this.shaking.bind(this) this.initSvgXml = this.initSvgXml.bind(this) this.strokeAnimation = this.strokeAnimation.bind(this) + this.loadScene = this.loadScene.bind(this) + this.loadSvgXml = this.loadSvgXml.bind(this) /** * Setup parameter */ @@ -70,6 +73,38 @@ export class SvgAnimation extends Two { sceneChildrenRestore() } } + /** + * Load SCENE + * @param {Two.Group} scene + */ + public loadScene(scene: Two.Group) { + this.clear() + scene.children.map((twoObj: Two.Object) => { + this.scene.add(twoObj.clone()) + }) + this.update() + } + /** + * Load SVGXML + * @param {string | SVGSVGElement} svgXml + */ + public loadSvgXml(svgXml: string | SVGSVGElement) { + const svgElement: SVGSVGElement | null = svgFormatting(svgXml) + if (!svgElement) return + const svgTwo: Two.Group = this.interpret(svgElement) + this.clear() + // get element width + // TODO: getelement Refactor + document.body.appendChild(svgElement) + const originWidth = svgElement.clientWidth + document.body.removeChild(svgElement) + this.scene.scale = this.width / originWidth + svgTwo.children.map((twoObj: Two.Object) => { + this.scene.add(twoObj.clone()) + }) + this.scene.center().translation.set(this.width / 2, this.height / 2) + this.update() + } /** * DrawingStart */ diff --git a/src/example/index.tsx b/src/example/index.tsx index 0e1425c57..28f5471b1 100644 --- a/src/example/index.tsx +++ b/src/example/index.tsx @@ -103,14 +103,10 @@ const Example = () => { ) const animationFrameUpdate = useCallback(() => { if (!svgAnimationRef.current || !svgDrawingRef.current) return - svgAnimationRef.current.clear() - // This is nesting dom - // animation.current.makeGroup(svgDrawingRef.current.scene.clone()) - svgDrawingRef.current.scene.children.map((twoObj: Two.Object) => { - if (!svgAnimationRef.current) return - svgAnimationRef.current.scene.add(twoObj.clone()) - }) - svgAnimationRef.current.update() + svgAnimationRef.current.loadScene(svgDrawingRef.current.scene) + // TODO: load svgXML example + // load SVGXML + // svgAnimationRef.current.loadSvgXml(svgDrawingRef.current.toSvgXml()) }, []) const clickDownload = useCallback( (extention: keyof typeof mimeTypeMap) => ( diff --git a/src/utils/svgFormatting.ts b/src/utils/svgFormatting.ts index b0458615e..525f4e907 100644 --- a/src/utils/svgFormatting.ts +++ b/src/utils/svgFormatting.ts @@ -4,9 +4,13 @@ * @param svgString * @returns {SVGSVGElement} */ -export const svgFormatting = (svgString: string): SVGSVGElement => { - const parser: DOMParser = new DOMParser() - const doc: Document = parser.parseFromString(svgString, 'image/svg+xml') +export const svgFormatting = ( + svgXML: string | SVGSVGElement +): SVGSVGElement => { + const doc: Document | SVGSVGElement = + typeof svgXML === 'string' + ? new DOMParser().parseFromString(svgXML, 'image/svg+xml') + : svgXML const svgEle: SVGSVGElement = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg'