Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="ru">
- <head>
- <meta charset='utf-8'>
- </head>
- <body>
- <canvas id="pic" width="500" height="500" style="border:1px solid red">
- </canvas><br/>
- <p id="mousePointerCoords">Coords will apear here</p>
- <button onclick="resetAll()">reset</button>
- <button onclick="checking=true;anim=draw()">animate</button>
- <button onclick="clearInterval(anim)">stop</button>
- <script type="text/javascript">
- let c=document.getElementById('pic');
- let ctx = c.getContext('2d');
- let mpc = document.getElementById('mousePointerCoords');
- let x, y, anim;
- let offx = 3, offy = 0;
- let colors = ['#dedede', '#f78a40', '#2b2b2b'];
- let polygons =
- [
- [[120, 400], [228, 400], [270, 355], [100, 355]],
- [[140, 355], [220, 355], [220, 335], [140, 335]],
- [[170, 335], [195, 335], [168, 310], [158, 310]]
- ];
- let polygonsForReset = polygons;
- let intersect = false;
- let border = false;
- let checking = false;
- //-----------------------------------------------------------------
- var plot = function(x, y) { //кисточка - ставит пиксель своего цвета
- if(isFinite(x) && isFinite(y)){
- setPixel(x,y, plot.color);
- }
- };
- function setPixel (x,y,c) { //ставит пиксель цвета с
- var p=ctx.createImageData(1,1);
- p.data[0]=c.r;
- p.data[1]=c.g;
- p.data[2]=c.b;
- p.data[3]=c.a;
- var data = ctx.getImageData(x, y, 1,1).data;
- if(data[3] <= p.data[3]) //сравниваем прозрачность
- ctx.putImageData(p,x,y);
- }
- function drawLine(x1, y1, x2, y2, color) {
- if(color) {
- plot.color = color;
- } else {
- plot.color = {r:0,g:0,b:0,a:255}; //цвет по умолчанию - черный
- }
- var deltaX = Math.abs(x2 - x1);
- var deltaY = Math.abs(y2 - y1);
- var signX = x1 < x2 ? 1 : -1;
- var signY = y1 < y2 ? 1 : -1;
- //
- var error = deltaX - deltaY;
- //
- plot(x2, y2);
- while(x1 != x2 || y1 != y2)
- {
- plot(x1, y1);
- var error2 = error * 2;
- //
- if(error2 > -deltaY)
- {
- error -= deltaY;
- x1 += signX;
- }
- if(error2 < deltaX)
- {
- error += deltaX;
- y1 += signY;
- }
- }
- }
- //-----------------------------------------------------------------
- c.onmousedown = function(e)
- {
- x = e.pageX-8;
- y = e.pageY-8;
- if(!checking) ctx.fillRect(x-1, y-1, 3,3);
- else
- {
- if(isInPolygons([x, y]))
- {
- alert('Попал');
- clearInterval(anim);
- ctx.fillStyle =
- pointAt(x, y);
- }
- else alert('Мимо');
- }
- mpc.innerText=x+' '+y;
- }
- function resetAll()
- {
- checking = false;
- polygons = polygonsForReset;
- ctx.clearRect(0,0,500,500);
- }
- function pointAt(x, y)
- {
- let c = ctx.fillStyle;
- ctx.fillStyle = '#000000';
- ctx.fillRect(x-1, y-1, 3,3);
- ctx.fillStyle = c;
- }
- function mul(p, b, e)
- {
- return Math.sign((p[0] - b[0])*(e[1] - b[1]) - (p[1] - b[1])*(e[0] - b[0]));
- }
- function typeofEdge(p, ebeg, eend)
- {
- let s = mul(p, ebeg, eend);
- if (s > 0) return ((eend[1] < p[1]) && (p[1] <= ebeg[1])) ? 1 : 0;
- else if (s < 0) return ((ebeg[1] < p[1]) && (p[1] <= eend[1])) ? 1 : 0;
- else return -1;
- }
- // /report/outline/user.php?id=26287&course=5274&mode=outline
- function isInPolygons(point)
- {
- for(poly of polygons)
- {
- console.log(poly)
- let l = poly.length;
- intersect = false;
- border = false;
- for(let i=0; i<l; i++)
- {
- if(typeofEdge(point, poly[i%l], poly[(i+1)%l])==1) intersect = !intersect;
- else if(typeofEdge(point, poly[i%l], poly[(i+1)%l])==-1) { border = true; break;}
- }
- if(border || intersect) return true;
- }
- return false;
- }
- function draw()
- {
- return setInterval(function()
- {
- ctx.clearRect(0,0,500,500);
- for(let i=0;i<polygons.length;i++)
- {
- polygons[i][0][0] += offx; polygons[i][0][1] += offy;
- for(let j=1;j<polygons[i].length;j++)
- {
- polygons[i][j][0]+=offx;
- polygons[i][j][1]+=offy;
- drawLine(polygons[i][j][0], polygons[i][j][1],polygons[i][(j+1)%polygons[i].length][0], polygons[i][(j+1)%polygons[i].length][1],{r:255, g:0,b:0,a:255})
- drawLine(polygons[i][0][0], polygons[i][0][1], polygons[i][1][0], polygons[i][1][1],{r:255, g:0,b:0,a:255})
- }
- }
- }, 100);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement