Skip to content

Commit

Permalink
feat: remove unuse svgDom
Browse files Browse the repository at this point in the history
  • Loading branch information
kmkzt committed May 28, 2019
1 parent 161b808 commit 5eefde8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/SvgDrawing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Two, { ConstructorParams } from 'two.js'
import { svgFormatting } from './utils/svgFormatting'
export interface DrawingOption extends ConstructorParams {
el: SvgDrawing['el']
penColor?: SvgDrawing['penColor']
Expand Down Expand Up @@ -53,11 +54,12 @@ export class SvgDrawing extends Two {
*/
public toSvgXml(): string | null {
const domElement: HTMLElement = (this.renderer as any).domElement
const svgElement: SVGSVGElement = svgFormatting(domElement.outerHTML)
if (!domElement) return null
return `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${
this.width
}" height="${this.height}" viewBox="0 0 ${this.width} ${this.height}">${
domElement.innerHTML
svgElement.innerHTML
}</svg>`
}
/**
Expand Down
26 changes: 26 additions & 0 deletions src/utils/svgFormatting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* remove <g>
* remove transform attribure
* @param svgString
* @returns {SVGSVGElement}
*/
export const svgFormatting = (svgString: string): SVGSVGElement => {
const parser: DOMParser = new DOMParser()
const doc: Document = parser.parseFromString(svgString, 'image/svg+xml')
const svgEle: SVGSVGElement = document.createElementNS(
'http://www.w3.org/2000/svg',
'svg'
)
const originSvgEle: SVGSVGElement | null = doc.querySelector('svg')
if (!originSvgEle) return svgEle
;['width', 'height', 'viewBox'].map((attr: string) => {
const attrValue: string | null = originSvgEle.getAttribute(attr)
if (attrValue) svgEle.setAttribute(attr, attrValue)
})
const pathEle: NodeListOf<SVGPathElement> = doc.querySelectorAll('path')
pathEle.forEach((path: SVGPathElement) => {
path.removeAttribute('transform')
svgEle.appendChild(path)
})
return svgEle
}

0 comments on commit 5eefde8

Please sign in to comment.