Skip to content

Commit 2ba53ac

Browse files
fix(snippets): use 'dom-to-image' instead 'html2canvas' #297 (#317)
1 parent 2cfbcf8 commit 2ba53ac

3 files changed

Lines changed: 18 additions & 47 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050
"codemirror": "^5.65.7",
5151
"codemirror-textmate": "^1.1.0",
5252
"date-fns": "^2.28.0",
53+
"dom-to-image": "^2.6.0",
5354
"electron-store": "^8.0.1",
5455
"floating-vue": "^2.0.0-beta.17",
5556
"fs-extra": "^10.0.1",
5657
"highlight.js": "^11.5.1",
57-
"html2canvas": "^1.4.1",
5858
"i18next": "^21.8.14",
5959
"i18next-fs-backend": "^1.1.4",
6060
"interactjs": "^1.10.11",
@@ -83,6 +83,7 @@
8383
"@commitlint/config-conventional": "^15.0.0",
8484
"@tsconfig/node14": "^1.0.1",
8585
"@types/codemirror": "^5.60.5",
86+
"@types/dom-to-image": "^2.6.4",
8687
"@types/estree": "^0.0.51",
8788
"@types/i18next-fs-backend": "^1.1.2",
8889
"@types/lowdb": "^1.0.11",

pnpm-lock.yaml

Lines changed: 12 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/components/screenshot/TheScreenshot.vue

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,19 @@
9494
</div>
9595
</div>
9696
</PerfectScrollbar>
97-
<div
98-
ref="previewRef"
99-
class="preview"
100-
/>
10197
</div>
10298
</template>
10399

104100
<script setup lang="ts">
105101
import { useAppStore } from '@/store/app'
106102
import hljs from 'highlight.js'
107-
import html2canvas from 'html2canvas'
108103
import interact from 'interactjs'
109104
import { computed, onMounted, ref, watch } from 'vue'
110105
import { useHljsTheme } from '@/composable'
111106
import { store, track, i18n } from '@/electron'
112107
import { useSnippetStore } from '@/store/snippets'
113108
import { useMagicKeys } from '@vueuse/core'
109+
import domToImage from 'dom-to-image'
114110
115111
const GUTTER_RIGHT_OFFSET = 10
116112
const GUTTER_WIDTH = 8
@@ -131,7 +127,6 @@ const { escape } = useMagicKeys()
131127
132128
const frameRef = ref<HTMLElement>()
133129
const snippetRef = ref<HTMLElement>()
134-
const previewRef = ref<HTMLElement>()
135130
const gutterRef = ref<HTMLElement>()
136131
137132
const gutterWidth = ref(GUTTER_WIDTH + 'px')
@@ -180,17 +175,13 @@ const init = () => {
180175
}
181176
182177
const onSaveScreenshot = async () => {
183-
const canvas = await html2canvas(snippetRef.value!, {
184-
backgroundColor: null
185-
})
186-
previewRef.value?.appendChild(canvas)
178+
const data = await domToImage.toPng(snippetRef.value!)
187179
188180
const a = document.createElement('a')
189-
const img = canvas.toDataURL('image/png')
190-
191-
a.href = img
181+
a.href = data
192182
a.download = `${props.name}.png`
193183
a.click()
184+
194185
track('snippets/create-screenshot')
195186
}
196187

0 commit comments

Comments
 (0)