ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
327
327
EMPFEHLEN
-
Schönen guten Nachmittag,
ich möchte ein simples Zeichenpad erstellen, das habe ich bereits von einem Tutorial übernommen und erweitert. (Mein Zeichenpad besteht auf der Grundlage dieses Tutorials: http://dev.opera.com/articles/view/h...nvas-painting/). Das ganze soll Synästhesie visualisieren und beim Zeichnen Sound abspielen. Für den Sound habe ich die Möglichkeit mp3 oder mini Daten zu erstellen. Der Code funktioniert, doch ich habe schon einiges versucht den Sound einzubinden doch irgendwas mache ich ziemlich falsch und finde nicht die richtigen Codes. Ich möchte keine Playerleiste wie z.B. mit Quicktime, sondern einen einzelnen Klang im Hintergrund wenn die Maus eine Form zeichnet, also wenn 'mousedown' aufgerufen wird. Die Audio-Schnipsel werden sehr kurz sein, also ca. nur 2-5 sec.
Für jede Form (Linie, Quadrat, Kreis etc.) soll es einen Ton geben (z.B. Klavierton, Geige, Tuba etc.).
Wie muss ich meinen Code aufbauen, mit welchen Elementen?
Vielen lieben Dank im Voraus!
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Paint</title> <style type="text/css" backgroundcolour="f3f8bf"> #container { position: relative; } #imageView { border: 1px solid #000; } #imageTemp { position: absolute; top: 1px; left: 1px; } </style> </head> <body> <div id="container"> <canvas id="imageView" width="1000" height="500" style="border: 1px solid #000000; background-color: #d4d4d2;" </canvas> </div> <p> <label>Zeichenelemente<br/> <select id="dtool"> <optgroup label="Kreis"> <option value="kreis1">Kreis Hellblau</option> <option value="kreis2">Kreis Blau</option> </optgroup> <optgroup label="Eckige Formen"> <option value="dreieck">Dreieck</option> <option value="eckig">Ecke</option> </optgroup> <optgroup label="Linien"> <option value="lineVertical">Vertikale</option> <option value="lineHorizontal">Horizontale</option> <option value="pencil">Fließende Linie</option> </optgroup> <option value="rect">Rechteck</option> </select></label></p> <script type="text/javascript"> if(window.addEventListener) { window.addEventListener('load', function () { var canvas, context, canvaso, contexto; var tool; var tool_default = 'line'; function init () { // Canvas Element canvaso = document.getElementById('imageView'); if (!canvaso) { alert('Error: I cannot find the canvas element!'); return; } if (!canvaso.getContext) { alert('Error: no canvas.getContext!'); return; } contexto = canvaso.getContext('2d'); if (!contexto) { alert('Error: failed to getContext!'); return; } // Temporäres Canvas var container = canvaso.parentNode; canvas = document.createElement('canvas'); if (!canvas) { alert('Error: I cannot create a new canvas element!'); return; } canvas.id = 'imageTemp'; canvas.width = canvaso.width; canvas.height = canvaso.height; container.appendChild(canvas); context = canvas.getContext('2d'); // Tool Select Input abholen var tool_select = document.getElementById('dtool'); if (!tool_select) { alert('Error: failed to get the dtool element!'); return; } tool_select.addEventListener('change', ev_tool_change, false); // Aktivieren des default tools if (tools[tool_default]) { tool = new tools[tool_default](); tool_select.value = tool_default; } // Event Listeners für Mausbewegungen canvas.addEventListener('mousedown', ev_canvas, false); canvas.addEventListener('mousemove', ev_canvas, false); canvas.addEventListener('mouseup', ev_canvas, false); } // Bestimmt die Mausposition relativ zum Canvas Objekt function ev_canvas (ev) { if (ev.layerX || ev.layerX == 0) { // Firefox ev._x = ev.layerX; ev._y = ev.layerY; } else if (ev.offsetX || ev.offsetX == 0) { // Opera ev._x = ev.offsetX; ev._y = ev.offsetY; } // Aufrufen des event handlers des tools var func = tool[ev.type]; if (func) { func(ev); } } // event handler für Änderung Zeichentools function ev_tool_change (ev) { if (tools[this.value]) { tool = new tools[this.value](); } } // Funktion zeichnet #imageTemp canvas über #imageView, nachdem #imageTemp gelöscht wird. Wird jedesmal aufgerufen, wenn eine Aktion beendet wird. function img_update () { contexto.drawImage(canvas, 0, 0); context.clearRect(0, 0, canvas.width, canvas.height); } // Objekt Variable enthält die Anwendung/Ausführung für jedes Zeichentool var tools = {}; // Linie tools.pencil = function () { var tool = this; this.started = false; // Startet die Funktion, wenn Maustaste gedrückt wird this.mousedown = function (ev) { context.beginPath(); context.moveTo(ev._x, ev._y); tool.started = true; }; this.mousemove = function (ev) { if (tool.started) { context.lineTo(ev._x, ev._y); context.strokeStyle="#2851af"; context.stroke(); } }; this.mouseup = function (ev) { if (tool.started) { tool.mousemove(ev); tool.started = false; img_update(); } }; }; // Quadrat tools.rect = function () { var tool = this; this.started = false; this.mousedown = function (ev) { tool.started = true; tool.x0 = ev._x; tool.y0 = ev._y; }; this.mousemove = function (ev) { if (!tool.started) { return; } var x = Math.min(ev._x, tool.x0), y = Math.min(ev._y, tool.y0), w = Math.abs(x - tool.x0, y - tool.y0), h = Math.abs(x - tool.x0, y - tool.y0); context.clearRect(0, 0, canvas.width, canvas.height); if (!w || !h) { return; } context.fillRect(x, y, w, h); context.fillStyle="#FF0000"; }; this.mouseup = function (ev) { if (tool.started) { tool.mousemove(ev); tool.started = false; img_update(); } }; }; // Dreieck tools.dreieck = function () { var tool = this; this.started = false; this.mousedown = function (ev) { tool.started = true; tool.x0 = ev._x; tool.y0 = ev._y; }; this.mousemove = function (ev) { if (!tool.started) { return; } context.clearRect(0, 0, canvas.width, canvas.height); var x = ev._x; var y = ev._y; context.beginPath(); context.moveTo(tool.x0, tool.y0); context.lineTo(ev._x, ev._y); context.lineTo(ev._x + 60, ev._y + 60); context.lineTo(tool.x0, tool.y0); context.closePath(); context.fillStyle="fdf402"; context.fill(); }; this.mouseup = function (ev) { if (tool.started) { tool.mousemove(ev); tool.started = false; img_update(); } }; }; // Kreis Blau (kreis2) tools.kreis2 = function() { var tool = this; this.started = false; this.mousedown = function (ev) { tool.started = true; tool.x0 = ev._x; tool.y0 = ev._y; }; this.mousemove = function (ev) { if (!tool.started) { return; } var x = Math.min(ev._x, tool.x0), y = Math.min(ev._y, tool.y0), w = Math.abs(ev._x - tool.x0), h = Math.abs(ev._y - tool.y0); context.clearRect(0, 0, canvas.width, canvas.height); context.fillStyle="#4f70ba"; context.fill(); context.beginPath(); context.arc(x, y, w, h, Math.PI*2, true); context.closePath(); } this.mouseup = function (ev) { if (tool.started) { tool.mousemove(ev); tool.started = false; img_update(); } }; }; // Kreis Hellblau (kreis1) tools.kreis1 = function() { var tool = this; this.started = false; this.mousedown = function (ev) { tool.started = true; tool.x0 = ev._x; tool.y0 = ev._y; }; this.mousemove = function (ev) { if (!tool.started) { return; } var x = Math.min(ev._x, tool.x0), y = Math.min(ev._y, tool.y0), w = Math.abs(ev._x - tool.x0), h = Math.abs(ev._y - tool.y0); context.clearRect(0, 0, canvas.width, canvas.height); context.fillStyle="#a4c5e0"; context.fill(); context.beginPath(); context.arc(x, y, w, h, Math.PI*2, true); context.closePath(); } this.mouseup = function (ev) { if (tool.started) { tool.mousemove(ev); tool.started = false; img_update(); } }; }; // horizontale linie. tools.lineHorizontal = function () { var tool = this; this.started = false; this.mousedown = function (ev) { tool.started = true; tool.x0 = ev._x; tool.y0 = ev._y; }; this.mousemove = function (ev) { if (!tool.started) { return; } context.clearRect(0, 0, canvas.width, canvas.height); context.beginPath(); context.moveTo(tool.x0, tool.y0); context.lineTo(ev._x, tool.y0); context.strokeStyle="000000"; context.stroke(); context.closePath(); }; this.mouseup = function (ev) { if (tool.started) { tool.mousemove(ev); tool.started = false; img_update(); } }; }; // vertikale linie. tools.lineVertical = function () { var tool = this; this.started = false; this.mousedown = function (ev) { tool.started = true; tool.x0 = ev._x; tool.y0 = ev._y; }; this.mousemove = function (ev) { if (!tool.started) { return; } context.clearRect(0, 0, canvas.width, canvas.height); context.beginPath(); context.moveTo(tool.x0, tool.y0); context.lineTo(tool.x0, ev._y); context.strokeStyle="white"; context.stroke(); context.closePath(); }; this.mouseup = function (ev) { if (tool.started) { tool.mousemove(ev); tool.started = false; img_update(); } }; }; // Ecken tools.eckig = function () { var tool = this; this.started = false; this.mousedown = function (ev) { tool.started = true; tool.x0 = ev._x; tool.y0 = ev._y; }; this.mousemove = function (ev) { if (!tool.started) { return; } context.clearRect(0, 0, canvas.width, canvas.height); context.beginPath(); context.moveTo(tool.x0, tool.y0); context.lineTo(ev._x, ev._y); context.lineTo(ev._x + 100, ev._y + 100); context.strokeStyle="yellow"; context.lineWidth="5"; context.stroke(); context.closePath(); }; this.mouseup = function (ev) { if (tool.started) { tool.mousemove(ev); tool.started = false; img_update(); } }; }; init(); }, false); } </script> </body> </html>
Ähnliche Themen
-
Beispiel-Sound per Klick im Hintergrund abspielen
Von sheeba1507 im Forum HTML & XHTMLAntworten: 1Letzter Beitrag: 25.07.11, 13:09 -
sound abspielen (mp3)
Von 3Cyb3r im Forum C/C++Antworten: 2Letzter Beitrag: 20.09.08, 11:15 -
Sound abspielen
Von CodeFatal im Forum VisualStudio & MFCAntworten: 0Letzter Beitrag: 16.08.06, 08:24 -
Hintergrund Sound Abspielen?
Von jackie05 im Forum HTML & XHTMLAntworten: 1Letzter Beitrag: 27.04.06, 07:13 -
Mit PHP Sound abspielen
Von Robert Steichele im Forum PHPAntworten: 1Letzter Beitrag: 25.11.05, 15:24





Zitieren
Login





