A
A
Alexander Sitnik2016-03-14 10:47:21
JavaScript
Alexander Sitnik, 2016-03-14 10:47:21

How to move a polygon?

Tell me, how to hang up such an event on the "Move" button so that the polygon moves 10 to the left, for example?
codepen.io/SitnikKsl/pen/dMpKbj

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Stolyarov, 2016-03-14
@SitnikKsl

var canvas;
var context;
var radius = 200;
var X0 = 250;
var Y0 = 200;

function redraw(){
  context.clearRect(0,0,canvas.width,canvas.height);
 
 context.beginPath();
 context.moveTo(X0 - 50, Y0 - 50);
 context.lineTo(X0 + 50, Y0 - 50);
 context.lineTo(X0 + 85, Y0);
 context.lineTo(X0 + 50, Y0 + 50);
 context.lineTo(X0 - 50, Y0 + 50);
 context.lineTo(X0 - 50, Y0 - 50);
 context.stroke();
  
}

window.onload = function() {
    
  canvas = document.getElementById("drawingCanvas");
       //Получаем наш холст по Id
context = canvas.getContext("2d");   
       //Холст в измерении 2d
       
       context.lineWidth = 3;
       //толщина линий
       context.strokeStyle = "rgb(210,106,68)";
       //цвет линий
       context.lineCap = "round";
       //стиль краев линий
       redraw();
     }

function move(x,y){
  
  X0+=x;
  Y0+=y;
  context.clearRect(0,0,canvas.width,canvas.height);
 redraw();
}

and hang a handler for the move procedure on the button
codepen.io/Ni55aN/pen/MyjXxQ

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question