
28/05/2015, 21:10
|
 | | | Fecha de Ingreso: abril-2007
Mensajes: 81
Antigüedad: 18 años Puntos: 3 | |
Respetar saltos de linea de TextArea en Canvas Hola amigos,
Hace unas horas estoy tratando de solucionar un problema que tengo, he buscado en todo lado y no hallo la solución. Espero que me pudan ayudar.
Usando el javascript y el html5 (canvas), eh logrado llenar una imagen con texto en la posición que deseo, pero el detalle esque al ingresar SALTOS DE LINEA en el textarea, este no son respetados por el canvas. Como podría hacer para que los saltos de linea sean respetados.
Aquí les dejo el archivo funcionando.
http://sullca.com/5p/
Código:
window.Meme = function(image, canvas, top, bottom) {
/*
Default top and bottom
*/
top = top || '';
bottom = bottom || '';
/*
Deal with the canvas
*/
// If it's nothing, set it to a dummy value to trigger error
if (!canvas)
canvas = 0;
// If it's a string, conver it
if (canvas.toUpperCase)
canvas = document.getElementById(canvas);
// If it's jQuery or Zepto, convert it
if (($) && (canvas instanceof $))
canvas = canvas[0];
// Throw error
if (!(canvas instanceof HTMLCanvasElement))
throw new Error('No canvas selected');
// Get context
var context = canvas.getContext('2d');
/*
Deal with the image
*/
// If there's no image, set it to a dummy value to trigger an error
if (!image)
image = 0;
// Convert it from a string
if (image.toUpperCase) {
var src = image;
image = new Image();
image.src = src;
}
// Set the proper width and height of the canvas
var setCanvasDimensions = function(w, h) {
canvas.width = w;
canvas.height = h;
};
setCanvasDimensions(image.width, image.height);
/*
Draw a centered meme string
*/
var drawText = function(text, topOrBottom, y) {
// Variable setup
topOrBottom = topOrBottom || 'top';
var fontSize = (canvas.height / 13);
var x = canvas.width / 2;
if (typeof y === 'undefined') {
y = fontSize;
if (topOrBottom === 'bottom')
y = canvas.height - 10;
if (topOrBottom === 'top')
y = canvas.height - 500;
}
// Should we split it into multiple lines?
if (context.measureText(text).width > (canvas.width * 0.8)) {
// Split word by word
var words = text.split(' ');
var wordsLength = words.length;
// Start with the entire string, removing one word at a time. If
// that removal lets us make a line, place the line and recurse with
// the rest. Removes words from the back if placing at the top;
// removes words at the front if placing at the bottom.
if (topOrBottom === 'top') {
var i = wordsLength;
while (i --) {
var justThis = words.slice(0, i).join(' ');
if (context.measureText(justThis).width < (canvas.width * 0.7)) {
drawText(justThis, topOrBottom, y);
drawText(words.slice(i, wordsLength).join(' '), topOrBottom, y + fontSize);
return;
}
}
}
else if (topOrBottom === 'bottom') {
for (var i = 0; i < wordsLength; i ++) {
var justThis = words.slice(i, wordsLength).join(' ');
if (context.measureText(justThis).width < (canvas.width * 0.7)) {
drawText(justThis, topOrBottom, y);
drawText(words.slice(0, i).join(' '), topOrBottom, y - fontSize);
return;
}
}
}
}
// Draw!
context.fillText(text, x, y, canvas.width * .9);
};
/*
Do everything else after image loads
*/
image.onload = function() {
// Set dimensions
setCanvasDimensions(this.width, this.height);
// Draw the image
context.drawImage(image, 0, 0);
// Set up text variables
context.fillStyle = 'white';
/*var fontSize = (canvas.height / 8);*/
context.font = /*fontSize + */'41px Philosopher';
context.textAlign = 'center';
// Draw them!
drawText(top, 'top');
drawText(bottom, 'bottom');
};
};
Gracias. |