Axes of a Cartesian plane changing according to the HTML5(canvas) + JAVASCRIPT zoom

Asked

Viewed 1,152 times

4

Guys, here’s the thing, I’m doing it with js + html5 and canvas a program to plot graphics: inserir a descrição da imagem aqui I would like to implement a zoom + and -, as in the image, and return to the original zoom by clicking '='. My difficulty is the following: after zooming in, the Cartesian axes 'x' and 'y' have to change their values is not ?

Example: http://jsxgraph.uni-bayreuth.de/wp/examples/, after pressing the '+' button the values of the axes change, another example is the google chart, after zooming the values of the axes change.

I wanted more or less this, I wanted the idea of how to do this, change the values of the axes according to the zoom, or ready-made examples, or some similar solution!!!

  • I was going through the documentation of canvas when I came across the property ctx.scale(2, 2), I believe you can work with her

  • Here is an answer to your question: http://answall.com/a/61161/14262

  • @Marcelobonifazio, that’s not what I want, I think Oce got it wrong, this zoom effect and Cale I already have, it’s working perfectly, what I want is, that after increasing the zoom or Scale (whatever) the values of the axes change, as in the example here: http://prntscr.com/7wh9dt after zooming in, the remaining axes changed the value http://prntscr.com/7wh9jt, and so on and so on, what I want is this.

  • I don’t think there’s any need to do this, but you can use the scale value to apply to each x and y this way: novoX = valorDaEscala + x ; novoY = valorDaEscala + y

  • I don’t know if I’d apply that very well, because my scale already starts at 50, how would that look? And at each click Scale is acrestado + 25, that is, it starts with 50, then 75 after 100 after 125...(considering what I’m clicking on '+'. I didn’t understand very well what you meant; Lemrando that, what is modified at each click is the scale, not the zoom, the zoom is always fixed,

  • Discard my last comment! I did not understand how the axis modifications would look according to the click, because my initial scale is 1, that is, ctx.Scale(1,1), this value is fixed, never changes. What I change with each click is the function 'line' scale, only the function 'line' is changed to its scale, not the whole screen. http://www.w3schools.com/tags/canvas_scale.asp ): notice that when the scale increases, the border of the tmb line changes, so it would be very 'scrotum' to increase the Scale of the whole screen, so I have a Scale variable, which determines the Scale only of the line of my function

Show 1 more comment

1 answer

1

Code adapted from: http://jsfiddle.net/ndYdk/7/

With the scale factor you apply only to the inside drawing(to the canvas inside points) without worrying about the canvas size.

function desenhar(escala, posicao){
  var canvas = document.getElementById("myCanvas");
  var context = canvas.getContext("2d");
  
  context.clearRect(0, 0, canvas.width, canvas.height);
  
  context.save();
  context.translate(posicao.x, posicao.y);
  context.scale(escala, escala);
  context.beginPath();
  context.moveTo(80, 50);
  context.bezierCurveTo(20,-100, -139, -80, -30, 50);
  context.closePath();

  context.fillStyle = 'blue';
  context.fill();

  context.stroke();
  context.restore();
}

var iniciar = (function(){
  var canvas = document.getElementById("myCanvas");

  var posicao = {
    x: canvas.width / 2,
    y: canvas.height / 2
  };

  var escala = 1.0;
  var fatorMultiplicacao = 0.8;

  document.getElementById("plus").addEventListener("click", function(){
    escala /= fatorMultiplicacao;
    desenhar(escala, posicao);
  }, false);

  document.getElementById("minus").addEventListener("click", function(){
    escala *= fatorMultiplicacao;
    desenhar(escala, posicao);
  }, false);

  desenhar(escala, posicao);
}());
<canvas id="myCanvas" style="width: 300px;"></canvas>
<input type="button" id="plus" value="+">
<input type="button" id="minus" value="-">

  • I understood Marc, but my question is not this, my chart he zoom, with this same example that you posted, my question is this, every click on '+', those numbers change value, as in the example above, every click on '+' (on the google chart) the numbers of the axes change, I want it, that they change, my graph gives the zoom, it grows quiet, only the numbers of the Cartesian axes do not follow the growth, they stay 'stopped', exe: http://prntscr.com/7wmchd, after 3 clicks on '+' > http://prntscr.com/7wmcza. Noticed that the numbers of the y axes have not changed ?

  • I wanted the value '1' that this in the first image, followed the growth, IE, at each click the '1' was rising along with the chart

  • Yes I understand, post the code of your function

  • http://pastebin.com/izNpcGPV, the codigo this half 'rough' up to pq I’m putting it a lot, but I’ll let it 'right'. The function showAxes(context), is that plots the axes 'x' and 'y' has 2 loops of repetition, one for each axis, to plot also the numbers. anything adds skype to me. Fillipe.santos6

  • Can you create an http://jsfiddle.net/ ja with the minimum HTML built in? it would be much easier to edit

  • I’m having trouble putting there, are some 4 js’s and css files

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.