Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
Javascript - Chart, moving points in pure js

I have this code, now by clicking on chart line I can move clicked point. It can be moved up, down, left, right and this is good but I should be able to move previos 20 points and next 20 points to do

something like this:

but looks like this:

You can also check here https://jsbin.com/qumihizoje/1/edit?html,js,output

var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var cw = canvas.width;
var ch = canvas.height;
var mouse = {};
var draggable = false;

context.lineWidth = 2;
context.strokeStyle = "blue";

var coordinates = [];

for (let i = 0; i < 300; i++) {
  coordinates.push({ x: i, y: getRandomInt(80, 85) });
}

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min;
}

canvas.addEventListener("mousedown", function(e) {
  handleMouseDown(e);
});

function handleMouseDown(e) {
  mouse = oMousePos(canvas, e);
  for (index = 0; index < coordinates.length; index++) {
    context.beginPath();
    context.arc( coordinates[index].x, coordinates[index].y, 5, 0, 2 * Math.PI );
    if (context.isPointInPath(mouse.x, mouse.y)) {
      draggable = index + 1;
      break;
    }
  }
}

function drawPolygon() {
  context.clearRect(0, 0, cw, ch);
  context.beginPath();
  context.moveTo(coordinates[0].x, coordinates[0].y);
  for (index = 1; index < coordinates.length; index++) {
    context.lineTo(coordinates[index].x, coordinates[index].y);
  }
  context.stroke();
}

canvas.addEventListener("mousemove", function(e) {
  if (draggable) {
    mouse = oMousePos(canvas, e);
    coordinates[draggable - 1].x = mouse.x;
    coordinates[draggable - 1].y = mouse.y;
    drawPolygon();
  }
});

canvas.addEventListener("mouseup", function(e) {
  if (draggable) {
    draggable = false;
  }
});

function oMousePos(canvas, evt) {
  var ClientRect = canvas.getBoundingClientRect();
  return {
    x: Math.round(evt.clientX - ClientRect.left),
    y: Math.round(evt.clientY - ClientRect.top)
  };
}

drawPolygon();
<canvas id="canvas"></canvas>

I'm moving only one point, how to fix it? I need to use pure js.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is not exactly what you are looking for but should get you closer...
I'm using a rolling average to move the previous 20 points and next 20 points.
You can use other strategies to move those points and get different type of curves, but since you have not tried anything this gives you a general idea of what is possible.

Result should look like:
enter image description here

var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var cw = canvas.width;
var ch = canvas.height;
var mouse = {};
var draggable = false;

context.lineWidth = 2;
context.strokeStyle = "blue";

canvas.addEventListener("mousemove", function(e) {
  if (draggable) {
    mouse = oMousePos(canvas, e);
    coordinates[draggable - 1].y = mouse.y;
    var max = Math.min(draggable + 20, coordinates.length)
    var min = Math.max(draggable - 20, 0)
    
    var ra = []
    ra.push(mouse.y)
    for (index = draggable; index < max; index++) {
      if (ra.length > 20) ra.shift()
      ra.push(clone[index].y)
      coordinates[index].y = ra.reduce((a, b) => a + b, 0)/ra.length
    }
    
    ra = []
    ra.push(mouse.y)
    for (index = (draggable-2); index > min; index--) {
      if (ra.length > 20) ra.shift()
      ra.push(clone[index].y)
      coordinates[index].y = ra.reduce((a, b) => a + b, 0)/ra.length
    }
    drawPolygon();
  }
});

canvas.addEventListener("mousedown", function(e) {
  handleMouseDown(e);
});

canvas.addEventListener("mouseup", function(e) {
  if (draggable) {
    draggable = false;
  }
});

var coordinates = [];

for (let i = 0; i < 300; i++) {
  coordinates.push({ x: i, y: getRandomInt(80, 85) });
}
var clone = coordinates.map((i) => ({ x: i.x, y: i.y }))

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min;
}

function handleMouseDown(e) {
  mouse = oMousePos(canvas, e);
  for (index = 0; index < coordinates.length; index++) {
    context.beginPath();
    context.arc( coordinates[index].x, coordinates[index].y, 5, 0, 2 * Math.PI );
    if (context.isPointInPath(mouse.x, mouse.y)) {
      draggable = index + 1;
      break;
    }
  }
}

function drawPolygon() {
  context.clearRect(0, 0, cw, ch);
  context.beginPath();
  context.moveTo(coordinates[0].x, coordinates[0].y);
  for (index = 1; index < coordinates.length; index++) {
    context.lineTo(coordinates[index].x, coordinates[index].y);
  }
  context.stroke();
}

function oMousePos(canvas, evt) {
  var ClientRect = canvas.getBoundingClientRect();
  return {
    x: Math.round(evt.clientX - ClientRect.left),
    y: Math.round(evt.clientY - ClientRect.top)
  };
}

drawPolygon();
<canvas id="canvas"></canvas>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda