質問編集履歴

2

fさふぁ

2019/01/29 00:56

投稿

tokuwgawa
tokuwgawa

スコア45

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,7 @@
6
6
  id="preview"
7
7
  :style="{
8
8
  border: 'solid 1px #000',
9
- 'background-color': this.selectedTemplate.background,
9
+ 'background-color': #ff0000
10
10
  }"
11
11
  >
12
12
  </canvas>

1

fa

2019/01/29 00:56

投稿

tokuwgawa
tokuwgawa

スコア45

title CHANGED
@@ -1,1 +1,1 @@
1
- HTML5】canvasを画像化する際にバックグランドカラーが表示されない【Javascript】
1
+ Nuxt.js】canvasを画像化する際にバックグランドカラーが表示されない【Javascript】
body CHANGED
@@ -1,14 +1,43 @@
1
1
  canvasを画像化してダウンロードする処理を作ったのですが、画像化されたファイルを見に行ってみるとバックグランドカラーだけが反映されず困っています。
2
2
  `png`形式にすると背景が透明色で保存され、`jpeg`で保存すると背景色が真っ黒で表示されます。
3
3
  ```html
4
+ <template>
4
5
  <canvas
5
6
  id="preview"
6
7
  :style="{
7
8
  border: 'solid 1px #000',
8
- 'background-color': #ff0000,
9
+ 'background-color': this.selectedTemplate.background,
9
10
  }"
10
11
  >
11
12
  </canvas>
13
+ </template>
14
+
15
+ //描画処理
16
+ <script>
17
+ export default {
18
+ mounted() {
19
+ this.ctx = document.getElementById('preview').getContext('2d')
20
+ this.drawCreative()
21
+ },
22
+ methods: {
23
+ drawCreative() {
24
+ this.ctx.canvas.width = 500
25
+ this.ctx.canvas.height = 500
26
+ this.ctx.beginPath()
27
+ this.ctx.clearRect(0, 0, 500, 500)
28
+ this.ctx.fill()
29
+ },
30
+ downloadCanvas() {
31
+ const canvasImage = document.getElementById('preview')
32
+ const link = document.createElement('a')
33
+ link.href = canvasImage.toDataURL('image/png')
34
+ link.download = 'hoge.png'
35
+ link.click()
36
+ },
37
+ },
38
+ }
39
+ </script>
40
+
12
41
  ```
13
42
 
14
43
  ```js