Foros del Web » Programando para Internet » Javascript »

Recortar canvas

Estas en el tema de Recortar canvas en el foro de Javascript en Foros del Web. Hola! He encontrado este código para recortar el canvas, atendiendo a los pixeles transparentes para el recorte. https://gist.github.com/remy/784508 Pero necesito ayuda, no consigo ponerlo en ...
  #1 (permalink)  
Antiguo 26/04/2013, 06:13
 
Fecha de Ingreso: agosto-2012
Mensajes: 14
Antigüedad: 11 años, 8 meses
Puntos: 0
Pregunta Recortar canvas

Hola!
He encontrado este código para recortar el canvas, atendiendo a los pixeles transparentes para el recorte.

https://gist.github.com/remy/784508

Pero necesito ayuda, no consigo ponerlo en funcionamiento.
Gracias!


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="keywords" content="">
<title></title>


</head>
<body>


<canvas id="c" width="200" height="100" >
Your browser does not support the HTML5 canvas tag.
</canvas>
<button onClick="trim(c)">Recortar</button>


<script>

var c=document.getElementById("c");
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);


</script>


<script>
function trim(c) {

var ctx = c.getContext('2d'),
copy = document.createElement('canvas').getContext('2d'),
pixels = ctx.getImageData(0, 0, c.width, c.height),
l = pixels.data.length,
i,
bound = {
top: null,
left: null,
right: null,
bottom: null
},
x, y;

for (i = 0; i < l; i += 4) {
if (pixels.data[i+3] !== 0) {
x = (i / 4) % c.width;
y = ~~((i / 4) / c.width);
if (bound.top === null) {
bound.top = y;
}
if (bound.left === null) {
bound.left = x;
} else if (x < bound.left) {
bound.left = x;
}
if (bound.right === null) {
bound.right = x;
} else if (bound.right < x) {
bound.right = x;
}
if (bound.bottom === null) {
bound.bottom = y;
} else if (bound.bottom < y) {
bound.bottom = y;
}
}
}
var trimHeight = bound.bottom - bound.top,
trimWidth = bound.right - bound.left,
trimmed = ctx.getImageData(bound.left, bound.top, trimWidth, trimHeight);
copy.canvas.width = trimWidth;
copy.canvas.height = trimHeight;
copy.putImageData(trimmed, 0, 0);
// open new window with trimmed image:
return copy.canvas;
}
</script>
</body>
</html>

Etiquetas: canvas, funcion, html, recortar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:38.