diff --git a/__tests__/snapshot/idraw/examples/test/api.html.jpg b/__tests__/snapshot/idraw/examples/test/api.html.jpg index 7a18841..d853bdc 100644 Binary files a/__tests__/snapshot/idraw/examples/test/api.html.jpg and b/__tests__/snapshot/idraw/examples/test/api.html.jpg differ diff --git a/packages/idraw/examples/features/index.html b/packages/idraw/examples/features/index.html index 730d6f9..f364a11 100644 --- a/packages/idraw/examples/features/index.html +++ b/packages/idraw/examples/features/index.html @@ -20,6 +20,9 @@
Redo
+
+ Download +
diff --git a/packages/idraw/examples/test/api.html b/packages/idraw/examples/test/api.html index 72dfd2c..fd991cc 100644 --- a/packages/idraw/examples/test/api.html +++ b/packages/idraw/examples/test/api.html @@ -15,13 +15,13 @@ background-size: 10px 10px, 10px 10px, 50px 50px, 50px 50px; } .list { - width: 910px; + width: 920px; overflow: hidden; border-top: 1px solid #f0f0f0; border-left: 1px solid #f0f0f0; } .box { - width: 300; + width: 302; min-height: 220; float: left; border-right: 1px solid #f0f0f0; @@ -52,6 +52,15 @@
exportDataURL('image/jpeg')
+
+
toDataURL('image/png')
+
+
+
toDataURL('image/jpeg')
+
+
+ +
@@ -157,11 +166,28 @@ imgJPG.src = jpgSrc; imgJPG.width = mount3.getBoundingClientRect().width; mount3.appendChild(imgJPG); + + setTimeout(() => { + const pngSrc = idraw.toDataURL('image/png'); + const mount4 = document.querySelector('#mount-4'); + const imgPNG = new window.Image(); + imgPNG.src = pngSrc; + imgPNG.width = mount4.getBoundingClientRect().width; + mount4.appendChild(imgPNG); + + const jpgSrc = idraw.toDataURL('image/jpeg'); + const mount5 = document.querySelector('#mount-5'); + const imgJPG = new window.Image(); + imgJPG.src = jpgSrc; + imgJPG.width = mount5.getBoundingClientRect().width; + mount5.appendChild(imgJPG); + }, 200); }, 100); })(); - + + \ No newline at end of file diff --git a/packages/idraw/src/index.ts b/packages/idraw/src/index.ts index 89c01d8..80004b5 100644 --- a/packages/idraw/src/index.ts +++ b/packages/idraw/src/index.ts @@ -9,7 +9,7 @@ import { _createOpts, _pushRecord, _keyboardWatcher, } from './names'; import { redo, undo } from './mixins/record'; -import { exportDataURL } from './mixins/file'; +import { exportDataURL, toDataURL } from './mixins/file'; import { copyElements, pasteElements, cutElements, deleteElements, keyArrowUp, keyArrowDown, keyArrowLeft, keyArrowRight, keyUndo, } from './mixins/keyboard'; @@ -45,6 +45,10 @@ class iDraw extends Core { return redo(this); } + toDataURL(type: 'image/png' | 'image/jpeg', quality?: number): string { + return toDataURL(this, type, quality); + } + async exportDataURL(type: 'image/png' | 'image/jpeg', quality?: number ): Promise { return exportDataURL(this, type, quality); } diff --git a/packages/idraw/src/mixins/file.ts b/packages/idraw/src/mixins/file.ts index 2c22274..56c1a9a 100644 --- a/packages/idraw/src/mixins/file.ts +++ b/packages/idraw/src/mixins/file.ts @@ -28,7 +28,17 @@ export async function exportDataURL( reject(err); } }); +} - + +export function toDataURL( + idraw: iDraw, + type: 'image/png' | 'image/jpeg', + quality?: number +): string { + const ctx = idraw.__getOriginContext2D(); + const canvas = ctx.canvas; + const dataURL: string = canvas.toDataURL(type, quality); + return dataURL; } \ No newline at end of file diff --git a/scripts/screen.config.js b/scripts/screen.config.js index 2eda813..be65401 100644 --- a/scripts/screen.config.js +++ b/scripts/screen.config.js @@ -6,7 +6,7 @@ const pageList = [ { path: 'core/examples/test/elements.html', w: 930, h: 810, delay: 800 }, { path: 'core/examples/test/api.html', w: 930, h: 1570, delay: 800 }, { path: 'core/examples/test/resource.html', w: 930, h: 540, delay: 800 }, - { path: 'idraw/examples/test/api.html', w: 930, h: 270, delay: 800 }, + { path: 'idraw/examples/test/api.html', w: 930, h: 540, delay: 800 }, ] module.exports = {