javascript zoom function

Asked

Viewed 1,333 times

3

I would like to create a zoom function inside a js extension file, I already have a canvas function, however in my drawing is coming out very small depending on the initial coordinates, so I would like something that when needed I could zoom in on the figure. My canvas function is:

function fcanvas(){
var scale = 1;
var originx = 0;
var originy = 0;
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath(); 
ctx.strokeStyle = '#000000'; 
ctx.lineWidth = 2; 
ctx.font='20px Arial';
ctx.beginPath();
ctx.moveTo(500,250) 
ctx.lineTo(500,250-(aa)); 
ctx.lineTo(500-(-bb),250 ); 
ctx.lineTo(500,250); 
ctx.font='12px Arial';
ctx.fillText("Tela 1000x500",20,20);
ctx.stroke(); 
}

I would like something easy, or something that is very detailed, because I’m starting to learn how to program and I don’t know many tags yet.

  • You want the canvas continue of the same size, only what is drawn on it increases in scale, or wants itself to canvas grow together with everything inside it (without you needing to change the way you draw it)? Both possibilities exist, it just wasn’t clear to me what you intend.

  • 1

    I want only what is drawn on the canvas to increase, leaving it the same size

1 answer

3


If you want the canvas whole increase in size, including your designed content, you can use the CSS property transform (which applies to any HTML element). If on the other hand what you want is that the content designed increase in size, just apply the transformation scale in the context of canvas (using the save and the restore to limit which part will be staggered).

Example with both transformations:

var zoomXY = 1;   // Zoom (mesmo pro x e pro y)
var escalaXY = 1; // Escala (mesma pro x e pro y)

function fcanvas(){
var scale = 1;
var originx = 0;
var originy = 0;
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
  
ctx.clearRect(0, 0, c.width, c.height); // Limpa o canvas antes de desenhar
ctx.save();                             // Salva as transformações
ctx.scale(escalaXY, escalaXY);          // Aplica a escala
  
ctx.beginPath(); 
ctx.strokeStyle = '#000000'; 
ctx.lineWidth = 2; 
ctx.font='20px Arial';
ctx.beginPath();
ctx.moveTo(500,250) 
ctx.lineTo(500,250-(aa)); 
ctx.lineTo(500-(-bb),250 ); 
ctx.lineTo(500,250); 
ctx.font='12px Arial';
ctx.fillText("Tela 1000x500",20,20);
ctx.stroke(); 
  
ctx.restore();                          // Reseta as transformações
}

function zoom(dv) {
  zoomXY += dv;
  document.getElementById("myCanvas").style.transform = "scale(" + zoomXY + ")";
}

function escala(dv) {
  escalaXY += dv;
  fcanvas();
}

var aa = 100;
var bb = 100;
fcanvas();
canvas {
    border: 4px solid black; /* Apenas para ajudar na visualização */

    transform: scale(1);   /* Transformação original (escala 100%) */
    transform-origin: 0 0;
}
Zoom no canvas:
<button onclick="zoom(0.1);">+</button>
<button onclick="zoom(-0.1);">-</button>
Zoom no conteúdo:
<button onclick="escala(0.1);">+</button>
<button onclick="escala(-0.1);">-</button>
<canvas id="myCanvas" width="1000", height="500"></canvas>

  • I’m not able to execute this code, copied as is there, and I can’t execute my scale function

  • @Isabela What is going wrong? As it is, it is necessary that the functions escala and zoom are in the top-level, if you are using jQuery for example and the functions are inside the $(document).ready(function() { ... }); then it is necessary to assign the click from the button from the JS code. Or the problem is another, like the functions are being called but something wrong happens? Any error message appeared in the log?

Browser other questions tagged

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