質問編集履歴

1

質問内容の修正

2021/12/13 06:15

投稿

Fukusuke0604
Fukusuke0604

スコア554

test CHANGED
File without changes
test CHANGED
@@ -2,8 +2,74 @@
2
2
 
3
3
 
4
4
 
5
+ Canvasに描画した画像ファイルをblob化し、axiosでPOSTの処理は以下の用に書いています、
6
+
5
7
  ```javascript
6
8
 
9
+ // Canvasのデータをblob化
10
+
11
+ saveCanvas: function (canvas_id) {
12
+
13
+ const type = "image/png";
14
+
15
+ const canvas = document.getElementById(canvas_id);
16
+
17
+ const dataurl = canvas.toDataURL("image/jpeg", 0.85);
18
+
19
+ const bin = atob(dataurl.split(",")[1]);
20
+
21
+ const buffer = new Uint8Array(bin.length);
22
+
23
+ for (let i = 0; i < bin.length; i++) {
24
+
25
+ buffer[i] = bin.charCodeAt(i);
26
+
27
+ }
28
+
29
+
30
+
31
+ const blob = new Blob([buffer.buffer], { type: type });
32
+
33
+
34
+
35
+ const data = new FormData();
36
+
37
+ data.append("photo", blob, "image.png"); //'photo'というkeyで保存
38
+
39
+ },
40
+
41
+
42
+
43
+ postCanvas: function () {
44
+
45
+ axios
46
+
47
+ .post("/api/photo", data, {
48
+
49
+ headers: { "content-type": "multipart/form-data" },
50
+
51
+ })
52
+
53
+ .then((res) => {
54
+
55
+ console.log("success");
56
+
57
+ })
58
+
59
+ .catch((error) => {
60
+
61
+ new Error(error);
62
+
63
+ });
64
+
65
+ },
66
+
67
+ ```
68
+
69
+
70
+
71
+ ```javascript
72
+
7
73
  var dataurl = canvas.toDataURL("image/jpeg", 0.85);
8
74
 
9
75
  ```
@@ -22,17 +88,127 @@
22
88
 
23
89
  ```
24
90
 
25
- として、canvasをJPEG変換し、そのBase64文字列をconst dataurlへセットしているのになぜ、 properties of null となってしまうのでしょうか?
91
+ として、canvas_id const canvasにセットしているのになぜ、「Cannot read properties of nullとなってしまうのでしょうか?
26
-
27
-
28
-
29
-
30
-
31
-
32
-
92
+
93
+
94
+
95
+
96
+
97
+
98
+
33
- Canvasに描画した画像ファイルをblob化し、axiosでPOSTの処理は以下のに書いています
99
+ vue.js 単一コンポーネント全体は以下のように書いています
100
+
34
-
101
+ ```
102
+
103
+ <template>
104
+
105
+ <div>
106
+
107
+ <div id="image_area">
108
+
109
+ <canvas ref="preview" id="preview"></canvas>
110
+
111
+ <input type="file" name="image" accept="image/*" ref="selectimage" @change="previewImage"/>
112
+
113
+ </div>
114
+
115
+ <p>
116
+
117
+ <input type="text" id="canvas_text" value="我輩は犬である" />
118
+
119
+ <button type="button" @click="drawText('preview', 'canvas_text')">
120
+
121
+ 文字を描く
122
+
123
+ </button>
124
+
125
+ </p>
126
+
127
+ <button @click="saveCanvas(),postCanvas()">アップロード</button>
128
+
129
+ </div>
130
+
131
+ </template>
132
+
133
+
134
+
35
- ```javascript
135
+ <script>
136
+
137
+ export default {
138
+
139
+
140
+
141
+ methods: {
142
+
143
+ // canvasの画像をinput type="file"にプレビュー
144
+
145
+ previewImage: function () {
146
+
147
+ let file = this.$refs.selectimage.files; //ファイル情報の取得
148
+
149
+ const canvas = this.$refs.preview; //canvasタグ
150
+
151
+ const fileReader = new FileReader();
152
+
153
+ fileReader.onload = function () {
154
+
155
+ const ctx = canvas.getContext("2d");
156
+
157
+ const image = new Image();
158
+
159
+ image.src = fileReader.result;
160
+
161
+ image.onload = function () {
162
+
163
+ canvas.width = image.width;
164
+
165
+ canvas.height = image.height;
166
+
167
+ ctx.drawImage(image, 0, 0);
168
+
169
+ };
170
+
171
+ };
172
+
173
+ fileReader.readAsDataURL(file[0]);
174
+
175
+ },
176
+
177
+
178
+
179
+ //キャンバスに文字を描く
180
+
181
+ drawText: function (canvas_id, text_id) {
182
+
183
+ const canvas = document.getElementById(canvas_id);
184
+
185
+ const ctx = canvas.getContext("2d");
186
+
187
+ const text = document.getElementById(text_id);
188
+
189
+ //文字のスタイルを指定
190
+
191
+ ctx.font = "32px serif";
192
+
193
+ ctx.fillStyle = "#404040";
194
+
195
+ //文字の配置を指定(左上基準にしたければtop/leftだが、文字の中心座標を指定するのでcenter
196
+
197
+ ctx.textBaseline = "center";
198
+
199
+ ctx.textAlign = "center";
200
+
201
+ //座標を指定して文字を描く(座標は画像の中心に)
202
+
203
+ const x = canvas.width / 2;
204
+
205
+ const y = canvas.height / 2;
206
+
207
+ ctx.fillText(text.value, x, y);
208
+
209
+ },
210
+
211
+
36
212
 
37
213
  // Canvasのデータをblob化
38
214
 
@@ -92,184 +268,6 @@
92
268
 
93
269
  },
94
270
 
95
- ```
96
-
97
-
98
-
99
-
100
-
101
- vue.js 単一コンポーネント全体は以下のように書いています。
102
-
103
- ```
104
-
105
- <template>
106
-
107
- <div>
108
-
109
- <div id="image_area">
110
-
111
- <canvas ref="preview" id="preview"></canvas>
112
-
113
- <input type="file" name="image" accept="image/*" ref="selectimage" @change="previewImage"/>
114
-
115
- </div>
116
-
117
- <p>
118
-
119
- <input type="text" id="canvas_text" value="我輩は犬である" />
120
-
121
- <button type="button" @click="drawText('preview', 'canvas_text')">
122
-
123
- 文字を描く
124
-
125
- </button>
126
-
127
- </p>
128
-
129
- <button @click="saveCanvas(),postCanvas()">アップロード</button>
130
-
131
- </div>
132
-
133
- </template>
134
-
135
-
136
-
137
- <script>
138
-
139
- export default {
140
-
141
-
142
-
143
- methods: {
144
-
145
- // canvasの画像をinput type="file"にプレビュー
146
-
147
- previewImage: function () {
148
-
149
- let file = this.$refs.selectimage.files; //ファイル情報の取得
150
-
151
- const canvas = this.$refs.preview; //canvasタグ
152
-
153
- const fileReader = new FileReader();
154
-
155
- fileReader.onload = function () {
156
-
157
- const ctx = canvas.getContext("2d");
158
-
159
- const image = new Image();
160
-
161
- image.src = fileReader.result;
162
-
163
- image.onload = function () {
164
-
165
- canvas.width = image.width;
166
-
167
- canvas.height = image.height;
168
-
169
- ctx.drawImage(image, 0, 0);
170
-
171
- };
172
-
173
- };
174
-
175
- fileReader.readAsDataURL(file[0]);
176
-
177
- },
178
-
179
-
180
-
181
- //キャンバスに文字を描く
182
-
183
- drawText: function (canvas_id, text_id) {
184
-
185
- const canvas = document.getElementById(canvas_id);
186
-
187
- const ctx = canvas.getContext("2d");
188
-
189
- const text = document.getElementById(text_id);
190
-
191
- //文字のスタイルを指定
192
-
193
- ctx.font = "32px serif";
194
-
195
- ctx.fillStyle = "#404040";
196
-
197
- //文字の配置を指定(左上基準にしたければtop/leftだが、文字の中心座標を指定するのでcenter
198
-
199
- ctx.textBaseline = "center";
200
-
201
- ctx.textAlign = "center";
202
-
203
- //座標を指定して文字を描く(座標は画像の中心に)
204
-
205
- const x = canvas.width / 2;
206
-
207
- const y = canvas.height / 2;
208
-
209
- ctx.fillText(text.value, x, y);
210
-
211
- },
212
-
213
-
214
-
215
- // Canvasのデータをblob化
216
-
217
- saveCanvas: function (canvas_id) {
218
-
219
- const type = "image/png";
220
-
221
- const canvas = document.getElementById(canvas_id);
222
-
223
- const dataurl = canvas.toDataURL("image/jpeg", 0.85);
224
-
225
- const bin = atob(dataurl.split(",")[1]);
226
-
227
- const buffer = new Uint8Array(bin.length);
228
-
229
- for (let i = 0; i < bin.length; i++) {
230
-
231
- buffer[i] = bin.charCodeAt(i);
232
-
233
- }
234
-
235
-
236
-
237
- const blob = new Blob([buffer.buffer], { type: type });
238
-
239
-
240
-
241
- const data = new FormData();
242
-
243
- data.append("photo", blob, "image.png"); //'photo'というkeyで保存
244
-
245
- },
246
-
247
-
248
-
249
- postCanvas: function () {
250
-
251
- axios
252
-
253
- .post("/api/photo", data, {
254
-
255
- headers: { "content-type": "multipart/form-data" },
256
-
257
- })
258
-
259
- .then((res) => {
260
-
261
- console.log("success");
262
-
263
- })
264
-
265
- .catch((error) => {
266
-
267
- new Error(error);
268
-
269
- });
270
-
271
- },
272
-
273
271
  },
274
272
 
275
273
  };