Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Fragen wir mal andersrum: Welchen Vorteil siehst du gegenüber einem drawImage?Wieso denkst du putimagedata ist nicht für Bewegung gedacht?
Quellcode
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 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> </head> <body> <canvas id='cv' width=1024 height=444></canvas> <img src='online.png' id='bunny' /> <script type="text/javascript">//<![CDATA[ window.onload=function(){ // Fills a flat triangle using bresenham algorithm. // Requires v2.y == v3.y && v2.x<=v3.x function fillFlatBaseTriangleLR(v1, v2, v3) { var vTmp1 = { x: v1.x, y: v1.y }; var vTmp2 = { x: v1.x, y: v1.y }; var d1 = { x: Math.abs(v2.x - v1.x), y: Math.abs(v2.y - v1.y) }; var d2 = { x: Math.abs(v3.x - v1.x), y: Math.abs(v3.y - v1.y) }; var sign1 = { x: sign(v2.x - v1.x), y: sign(v2.y - v1.y) }; var sign2 = { x: sign(v3.x - v1.x), y: sign(v3.y - v1.y) }; var itAxe1 = 'y', itConvAxe1 = 'x'; var itAxe2 = 'y', itConvAxe2 = 'x'; var tmp = 0; if (d1.y > d1.x) { // swap axes itAxe1 = 'x'; itConvAxe1 = 'y'; } if (d2.y > d2.x) { // swap axes itAxe2 = 'x'; itConvAxe2 = 'y'; } var e1 = 2 * d1[itAxe1] - d1[itConvAxe1]; var e2 = 2 * d2[itAxe2] - d2[itConvAxe2]; var limit = d1[itConvAxe1]; for (var i = 0; i <= limit; i++) { // drawHLine(vTmp1.x, vTmp2.x, vTmp2.y); var relY1 = Math.abs((vTmp1[itConvAxe1] - v1[itConvAxe1]) / d1[itConvAxe1]); var relY2 = Math.abs((vTmp2[itConvAxe2] - v1[itConvAxe2]) / d2[itConvAxe2]); // console.log(relY1, relY2); drawHLineText(vTmp1.x, vTmp2.x, vTmp2.y, v1.u + (v2.u - v1.u) * relY1, v1.v + (v2.v - v1.v) * relY1, v1.u + (v3.u - v1.u) * relY2, v1.v + (v3.v - v1.v) * relY2); while (e1 >= 0) { vTmp1[itAxe1] += sign1[itAxe1]; e1 = e1 - 2 * d1[itConvAxe1]; } vTmp1[itConvAxe1] += sign1[itConvAxe1]; e1 = e1 + 2 * d1[itAxe1]; // have right line reach same y as left line. while (vTmp2.y != vTmp1.y) { while (e2 >= 0) { vTmp2[itAxe2] += sign2[itAxe2]; e2 = e2 - 2 * d2[itConvAxe2]; } vTmp2[itConvAxe2] += sign2[itConvAxe2]; e2 = e2 + 2 * d2[itAxe2]; } } } function fillFlatBaseTriangle(v1, v2, v3) { if (v2.x > v3.x) { return fillFlatBaseTriangleLR(v1, v3, v2); } else { return fillFlatBaseTriangleLR(v1, v2, v3); } } function rasterizeTriangle(v1, v2, v3) { points[0] = v1; points[1] = v2; points[2] = v3; points.sort(sortOnY); v1 = points[0]; v2 = points[1]; v3 = points[2]; if (v2.y == v3.y) { return fillFlatBaseTriangle(v1, v2, v3); } if (v1.y == v2.y) { return fillFlatBaseTriangle(v3, v1, v2); } // split the triangle var v4x = (v1.x + ((v2.y - v1.y) / (v3.y - v1.y)) * (v3.x - v1.x)); var v4 = { x: v4x, y: v2.y, u: 0, v: 0 }; var relLength = lengthP(v1, v4) / lengthP(v1, v3); v4.u = v1.u + relLength * (v3.u - v1.u); v4.v = v1.v + relLength * (v3.v - v1.v); fillFlatBaseTriangle(v1, v2, v4); fillFlatBaseTriangle(v3, v2, v4); return; } var points = [null, null, null]; function sortOnY(a, b) { return a.y - b.y }; // -------------------------------------------- // -------------------------------------------- var canvas = document.getElementById('cv'); var ctx = canvas.getContext('2d'); ctx.translate(0.5, 0.5); function sign(x) { if (x == 0) return 0; return x > 0 ? 1 : -1; } function sq(x) { return x * x; } function lengthP(a, b) { return Math.sqrt(sq(a.x - b.x) + sq(a.y - b.y)); } function drawHLine(x1, x2, y) { ctx.beginPath(); ctx.moveTo(x1, y); ctx.lineTo(x2, y); ctx.stroke(); } var prevY = -1; function drawHLineText(x1, x2, y, u1, v1, u2, v2) { if (prevY == y) return; prevY = y; x1 = 0 | x1; x2 = 0 | x2; y = 0 | y; var du = (u2 - u1); var dv = (v2 - v1); var len = (x2 - x1) + 1; du /= len; dv /= len; for (var i = 0; len; len--, i++) { ctx.drawImage(textureCanvas, u1 + i * du, v1 + i * dv, du, dv, x1 + i, y, 1, 1); } } var textureCanvas = document.createElement('canvas'); var bunny = new Image(); bunny.src= 'online.png'; textureCanvas.width = bunny.width; textureCanvas.height = bunny.height; var textCtx = textureCanvas.getContext('2d'); textCtx.drawImage(bunny, 0, 0); document.body.appendChild(textureCanvas); var v1 = { x: 50, y: 00, u: 0, v: 0 }; var v2 = { x: bunny.width-50, y: 0, u: bunny.width, v: 0 }; var v3 = { x: 0, y: 444, u: 0, v: bunny.height }; var v5 = { x: bunny.width, y: 444, u: bunny.width, v: bunny.height }; textCtx.strokeRect(v1.u, v1.v, v2.u - v1.u, v2.v - v1.v); textCtx.strokeRect(v1.u, v1.v, v3.u - v1.u, v3.v - v1.v); var mouse = { x: 0, y: 0, down: false, dragging: null }; var cornerPoints = [v1, v2, v3, v5]; function nearestPoint(pt) { var dist = 1e12, res = null; for (var __i = 0; __i < cornerPoints.length; __i++) { var thisDist = lengthP(pt, cornerPoints[__i]); console.log('skk ' + __i + ' dst ' + thisDist + ' ' + cornerPoints[__i].x + ' ' + cornerPoints[__i].y); if (thisDist < dist) { console.log('fnd' + thisDist); dist = thisDist; res = cornerPoints[__i]; } } if (dist < 8) return res; return null; } function setupMouse(canvas, preventDefault) { var rectLeft, rectTop; var cssScaleX = canvas.width / canvas.offsetWidth; var cssScaleY = canvas.height / canvas.offsetHeight; var hook = canvas.addEventListener.bind(canvas); var mouseDown = updateMouseStatus.bind(null, true); var mouseUp = updateMouseStatus.bind(null, false); var mouseOut = function () { mouse.down = false; }; hook('mousedown', mouseDown); hook('mouseup', mouseUp); hook('mouseout', mouseOut); hook('mousemove', updateCoordinates); hook('scroll', updateRect); function updateMouseStatus(b, e) { var previousMouseDown = mouse.down; mouse.down = b; updateCoordinates(e); if (!previousMouseDown && b) { var pt = nearestPoint(mouse); if (pt) mouse.dragging = pt; console.log(mouse.x, mouse.y, mouse.dragging); changed = true; } if (previousMouseDown && b && mouse.dragging) { mouse.dragging.x = mouse.x; mouse.dragging.y = mouse.y; changed = true; } if (!b) mouse.dragging = false; if (preventDefault) { e.stopPropagation(); e.preventDefault(); } } function updateCoordinates(e) { mouse.x = (e.clientX - rectLeft) * cssScaleX; mouse.y = (e.clientY - rectTop) * cssScaleY; if (mouse.dragging) { mouse.dragging.x = mouse.x; mouse.dragging.y = mouse.y; changed = true; } } function updateRect() { var rect = canvas.getBoundingClientRect(); rectLeft = rect.left; rectTop = rect.top; } updateRect(); }; var changed = true; setupMouse(canvas, true); rasterizeTriangle(v1, v2, v3); function draw() { requestAnimationFrame(draw); if (!changed) return; changed = false; ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = '#000'; ctx.fillText('Grab the borders and move the mouse', 20, 10); rasterizeTriangle(v1, v2, v3); rasterizeTriangle(v5, v2, v3); if (mouse.dragging) { ctx.beginPath(); ctx.arc(mouse.x, mouse.y, 5, 0, 6.28); ctx.stroke(); } }; draw(); } </script> </body> </html>
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »BlueCobold« (31.05.2017, 21:44)
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Werbeanzeige