質問編集履歴

1

ehnko

2019/01/18 00:50

投稿

tokuwgawa
tokuwgawa

スコア45

test CHANGED
File without changes
test CHANGED
@@ -231,3 +231,247 @@
231
231
  [リンク内容](https://codepen.io/anon/pen/REyjVp?editors=1111)
232
232
 
233
233
  [リンク内容](http://www.html5.jp/canvas/how6.html)
234
+
235
+ # 追記
236
+
237
+ yhgさんの通りコードを書きましたが、エラーが表示されるようになりました。
238
+
239
+ ```
240
+
241
+ Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.
242
+
243
+ ```
244
+
245
+
246
+
247
+ ```
248
+
249
+ //image.vue
250
+
251
+ <template>
252
+
253
+ <div>
254
+
255
+ <div v-for="image in layer">
256
+
257
+ <img v-show="uploadedImage" :src="uploadedImage" />
258
+
259
+ <input type="file" ref="file" @change="onFileChange"/>
260
+
261
+ <el-button @click="setImage(image)">反映</el-button>
262
+
263
+ </div>
264
+
265
+ </div>
266
+
267
+ </template>
268
+
269
+
270
+
271
+ <script>
272
+
273
+ export default {
274
+
275
+ props: ['tmp_template'],
276
+
277
+ data() {
278
+
279
+ return {
280
+
281
+ uploadedImage: ""
282
+
283
+ }
284
+
285
+ },
286
+
287
+ computed: {
288
+
289
+ layer() {
290
+
291
+ return this.tmp_template.layers.filter(function(layer) {
292
+
293
+ if (layer.type === 'image') {
294
+
295
+ return layer
296
+
297
+ }
298
+
299
+ })
300
+
301
+ },
302
+
303
+ },
304
+
305
+ methods: {
306
+
307
+ //今回追加文
308
+
309
+ getDataURI(file) {
310
+
311
+ return new Promise((resolve, reject) => {
312
+
313
+ const reader = new FileReader();
314
+
315
+ reader.readAsDataURL(file)
316
+
317
+ reader.onload = () => resolve(reader, result)
318
+
319
+ reader.onerror = error => reject(error)
320
+
321
+ })
322
+
323
+ },
324
+
325
+ viewValue() {
326
+
327
+ //previe.vueの関数
328
+
329
+ this.$emit('viewValue')
330
+
331
+ },
332
+
333
+ onFileChange(file) {
334
+
335
+ const elFile = this.$refs.file
336
+
337
+ const files = elFile[0].files[0]
338
+
339
+    //関数代入
340
+
341
+ this.uploadedImage = this.getDataURI(files)
342
+
343
+ },
344
+
345
+ setImage(image) {
346
+
347
+ image.value = this.uploadedImage
348
+
349
+ this.viewValue()
350
+
351
+ }
352
+
353
+ },
354
+
355
+ }
356
+
357
+ </script>
358
+
359
+ ```
360
+
361
+
362
+
363
+ ```
364
+
365
+ //preview.vue
366
+
367
+ <template>
368
+
369
+ <div>
370
+
371
+ <div style="width:1360px;height:250px;padding:50px;border:solid 1px #DDD;background-color:#FFFFFF;overflow:scroll">
372
+
373
+ <canvas id="preview" :style="{
374
+
375
+ border: 'solid 1px #000',
376
+
377
+ 'background-color': '#FFFFFF',
378
+
379
+ }">
380
+
381
+ </canvas>
382
+
383
+ </div>
384
+
385
+ </div>
386
+
387
+
388
+
389
+
390
+
391
+ </template>
392
+
393
+
394
+
395
+
396
+
397
+ <script>
398
+
399
+ export default {
400
+
401
+ data() {
402
+
403
+ return {}
404
+
405
+ },
406
+
407
+ props: ['tmp_template'],
408
+
409
+ mounted() {
410
+
411
+ this.ctx = this.$el.firstChild.firstElementChild.getContext('2d')
412
+
413
+ this.drawCreative()
414
+
415
+ },
416
+
417
+ methods: {
418
+
419
+ //Canvas生成
420
+
421
+ drawCreative() {
422
+
423
+ this.ctx.canvas.width = this.tmp_template.width
424
+
425
+ this.ctx.canvas.height = this.tmp_template.height
426
+
427
+ this.ctx.beginPath()
428
+
429
+ this.ctx.clearRect(0, 0, this.tmp_template.width, this.tmp_template.height)
430
+
431
+ this.tmp_template.layers.forEach(layer => {
432
+
433
+ this.ctx.strokeStyle = this.setTypeColor(layer.type)
434
+
435
+ if(layer.type === 'text' && layer.value) {
436
+
437
+ this.ctx.fillText(layer.value, layer.x_position, layer.y_position - -10)
438
+
439
+ }
440
+
441
+ else if(layer.type === 'image' && layer.value)
442
+
443
+ {
444
+
445
+       //画像表示処理
446
+
447
+ let image1 = new Image()
448
+
449
+ let reader = new FileReader()
450
+
451
+ image1.src = layer.value.name
452
+
453
+ reader.onload = (e) => {
454
+
455
+ this.ctx.drawImage(image1, 0, 0)
456
+
457
+ };
458
+
459
+ reader.readAsDataURL(layer.value);
460
+
461
+ }
462
+
463
+ this.ctx.strokeRect(layer.x_position, layer.y_position, layer.width, layer.height)
464
+
465
+ })
466
+
467
+ this.ctx.fill()
468
+
469
+ },
470
+
471
+ },
472
+
473
+ }
474
+
475
+ </script>
476
+
477
+ ```