質問編集履歴

2

code 部分を質問画面上で記しました

2018/03/08 03:38

投稿

xmg
xmg

スコア20

test CHANGED
File without changes
test CHANGED
@@ -42,6 +42,10 @@
42
42
 
43
43
  うまく行く部分のコード**
44
44
 
45
+ ```ここに言語を入力 html
46
+
47
+ コード
48
+
45
49
 
46
50
 
47
51
  <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
@@ -170,12 +174,16 @@
170
174
 
171
175
 
172
176
 
177
+ ```
178
+
173
179
  ---**
174
180
 
175
181
  合成したら動かないコード**
176
182
 
177
183
 
178
184
 
185
+ ```ここに言語を入力 html
186
+
179
187
 
180
188
 
181
189
  <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
@@ -366,6 +374,8 @@
366
374
 
367
375
 
368
376
 
377
+ ```
378
+
369
379
 
370
380
 
371
381
  よろしくお願いいたします。

1

コード実例を追加いたしました。

2018/03/08 03:38

投稿

xmg
xmg

スコア20

test CHANGED
File without changes
test CHANGED
@@ -35,3 +35,337 @@
35
35
  https://qiita.com/haribote/items/b17d46b9679ce2fb2712
36
36
 
37
37
  一発芸!SVGでHTMLを画像化する
38
+
39
+
40
+
41
+ ---**
42
+
43
+ うまく行く部分のコード**
44
+
45
+
46
+
47
+ <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
48
+
49
+
50
+
51
+
52
+
53
+ </head>
54
+
55
+
56
+
57
+ <body>
58
+
59
+
60
+
61
+ <section class="">
62
+
63
+
64
+
65
+ <canvas id="cv1" width="360" height="240">
66
+
67
+ </canvas>
68
+
69
+
70
+
71
+
72
+
73
+ <form>
74
+
75
+ <input type="button" value="change color" onclick="chgCol()">
76
+
77
+ <input type="button" value="convert image" onclick="chgImg()">
78
+
79
+ </form>
80
+
81
+
82
+
83
+ <div>
84
+
85
+ <img id="newImg">
86
+
87
+ </div>
88
+
89
+ </section>
90
+
91
+
92
+
93
+
94
+
95
+ <script>
96
+
97
+
98
+
99
+ var cvs = document.getElementById("cv1");
100
+
101
+ var ctx = cvs.getContext("2d");
102
+
103
+
104
+
105
+ //canvas .. draw random color stripes
106
+
107
+
108
+
109
+ function chgCol()
110
+
111
+ {
112
+
113
+ for ( var i = 0 ; i < 9 ; i++ )
114
+
115
+ {
116
+
117
+ var r = Math.floor(Math.random()*256);
118
+
119
+ var g = Math.floor(Math.random()*256);
120
+
121
+ var b = Math.floor(Math.random()*256);
122
+
123
+
124
+
125
+ ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
126
+
127
+ ctx.fillRect( 40*i, 0, 40, 240 );
128
+
129
+ }
130
+
131
+ }
132
+
133
+
134
+
135
+ //canvas data to other image format
136
+
137
+
138
+
139
+ function chgImg()
140
+
141
+ {
142
+
143
+ var pngData = cvs.toDataURL() ;
144
+
145
+ document.getElementById("newImg").src = pngData ;
146
+
147
+ }
148
+
149
+
150
+
151
+ // ref: http://www.pori2.net/html5/Canvas/150.html
152
+
153
+ //
154
+
155
+ // toDataURL(imgage/png) = toDataURL()
156
+
157
+ // toDataURL(image/svg+xml)
158
+
159
+ // toDataURL(image/jpeg)
160
+
161
+
162
+
163
+ </script>
164
+
165
+
166
+
167
+ </body>
168
+
169
+ </html>
170
+
171
+
172
+
173
+ ---**
174
+
175
+ 合成したら動かないコード**
176
+
177
+
178
+
179
+
180
+
181
+ <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
182
+
183
+
184
+
185
+ <script>
186
+
187
+
188
+
189
+ function changeColor()
190
+
191
+ {
192
+
193
+ document.getElementById("box01" ).style.backgroundColor = "orange" ;
194
+
195
+ }
196
+
197
+
198
+
199
+ </script>
200
+
201
+
202
+
203
+
204
+
205
+ </head>
206
+
207
+
208
+
209
+ <body>
210
+
211
+
212
+
213
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 460 380" width="460" height="380">
214
+
215
+
216
+
217
+ <style>
218
+
219
+ .w00 { width: 100px;
220
+
221
+ }
222
+
223
+ .h00 { height: 50px;
224
+
225
+ }
226
+
227
+ .green0 { background-color: green ;
228
+
229
+ }
230
+
231
+ .blue3 { background-color: lightblue;
232
+
233
+ }
234
+
235
+ .wrap3 { padding: 15px ;
236
+
237
+ }
238
+
239
+ </style>
240
+
241
+
242
+
243
+ <g>
244
+
245
+ <foreignObject width='100%' height='100%' >
246
+
247
+
248
+
249
+ <div xmlns="http://www.w3.org/1999/xhtml">
250
+
251
+
252
+
253
+
254
+
255
+ <section class="wrap3 ">
256
+
257
+ <div id="box01" class="w00 h00 green0 " >
258
+
259
+ </div>
260
+
261
+ <div id="box02" class="w00 h00 blue3 " >
262
+
263
+ </div>
264
+
265
+
266
+
267
+ </section>
268
+
269
+
270
+
271
+
272
+
273
+ </div>
274
+
275
+
276
+
277
+ </foreignObject>
278
+
279
+ </g>
280
+
281
+ </svg>
282
+
283
+
284
+
285
+
286
+
287
+ <br>
288
+
289
+ <button class= " " onclick= "changeColor() " > change color
290
+
291
+ </button>
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+ <section class="">
300
+
301
+
302
+
303
+
304
+
305
+ <form>
306
+
307
+ <input type="button" value="convert image" onclick="chgImg()">
308
+
309
+ </form>
310
+
311
+
312
+
313
+ <div>
314
+
315
+ <img id="newImg">
316
+
317
+ </div>
318
+
319
+ </section>
320
+
321
+
322
+
323
+
324
+
325
+
326
+
327
+ <footer>
328
+
329
+ <script>
330
+
331
+
332
+
333
+
334
+
335
+ //canvas data to other image format
336
+
337
+ const canvasEl = document.getElementById('canvas');
338
+
339
+
340
+
341
+ function chgImg()
342
+
343
+ {
344
+
345
+ let pngData = canvasEl.toDataURL() ;
346
+
347
+
348
+
349
+ document.getElementById("newImg").src = pngData ;
350
+
351
+ }
352
+
353
+
354
+
355
+ </script>
356
+
357
+
358
+
359
+ </footer>
360
+
361
+
362
+
363
+ </body>
364
+
365
+ </html>
366
+
367
+
368
+
369
+
370
+
371
+ よろしくお願いいたします。