Foros del Web » Programando para Internet » Javascript »

[SOLUCIONADO] Cambiar imagen canvas html5 javascript?

Estas en el tema de Cambiar imagen canvas html5 javascript? en el foro de Javascript en Foros del Web. Hola me preguntaba como hacer para qué la imagen de una canvas cambie al seleccionar una opcion de mi menu select() pueden ver lo que ...
  #1 (permalink)  
Antiguo 14/04/2013, 11:45
 
Fecha de Ingreso: abril-2013
Mensajes: 42
Antigüedad: 11 años
Puntos: 0
Pregunta Cambiar imagen canvas html5 javascript?

Hola me preguntaba como hacer para qué la imagen de una canvas cambie al seleccionar una opcion de mi menu select()

pueden ver lo que estoy haciendo http://www.zombie-revolution.net/memes/index.php <- aqui

mi código del javascript que crea el canvas:

Código Javascript:
Ver original
  1. //<![CDATA[
  2. meme_seleccionado = "memes/meme_5.jpg"; // creo que esto debe cambiar pero nose como hacerlo =/
  3. $(window).load(function(){
  4. $(function() {
  5.  
  6.   var cvs         = $("canvas"),
  7.       cvsWidth    = cvs.width(),
  8.       cvsHeight   = cvs.height(),
  9.       ctx         = cvs[0].getContext("2d"),
  10.       top         = $("#top"),
  11.       bottom      = $("#bottom");
  12.   function writeCaptionDOWN( text, y ) {
  13.     var size = 100;
  14.  
  15.     do {
  16.       size--;
  17.       ctx.font = size + 'px Impact';
  18.       ctx.lineWidth = size / 32;
  19.     } while ( ctx.measureText( text ).width > cvsWidth )
  20.  
  21.     ctx.fillText( text, cvsWidth / 2, y-20 );
  22.     ctx.strokeText( text, cvsWidth / 2, y-20 );
  23.  
  24.   }
  25.  
  26.   function writeCaptionTOP( text, y ) {
  27.     var size = 100;
  28.  
  29.     do {
  30.       size--;
  31.       ctx.font = size + 'px Impact';
  32.       ctx.lineWidth = size / 32;
  33.     } while ( ctx.measureText( text ).width > cvsWidth )
  34.  
  35.     ctx.fillText( text, cvsWidth / 2, y );
  36.     ctx.strokeText( text, cvsWidth / 2, y );
  37.  
  38.   }
  39.  
  40.   function writeCaptionEx( text, y ) {
  41.     var size = 20;
  42.  
  43.     do {
  44.       size--;
  45.       ctx.font = size + 'px Impact';
  46.       ctx.lineWidth = size / 50;
  47.     } while ( ctx.measureText( text ).width > cvsWidth )
  48.  
  49.     ctx.fillText( text, 720 / 2, y-2 );
  50.     ctx.strokeText( text, 720 / 2, y-2 );
  51.  
  52.   }
  53.  
  54.   // Setup basic canvas settings
  55.   $.extend( ctx, {
  56.     strokeStyle : "#000000",
  57.     textAlign : 'center',
  58.     fillStyle : "#ffffff",
  59.     lineCap : "round"
  60.   })
  61.  
  62.   $("<img />").load(function() {
  63.  
  64.       var img = this;
  65.  
  66.     $(document.body).on("keyup", function() {
  67.         var topText = top.val(),
  68.             bottomText = bottom.val();
  69.         ctx.clearRect( 0, 0, cvsWidth, cvsHeight );
  70.         ctx.drawImage( img, 0, 0 );
  71.         ctx.textBaseline = 'top';
  72.         writeCaptionTOP( topText, 0 )
  73.  
  74.         ctx.textBaseline = 'bottom';
  75.         writeCaptionDOWN( bottomText, cvsHeight )
  76.         ctx.textBaseline = 'creditos';
  77.         writeCaptionEx("facebook.com/DarkuzsSanchez", 380)
  78.  
  79.       }).trigger("keyup");
  80.  
  81.     })
  82.     .attr("src", meme_seleccionado);
  83. });
  84.  
  85. });//]]>

el código del menu:

Código PHP:
<center>
<p>
<h1>Selecciona Personaje</h1>
<?php
    $dir 
"memes";//directorio de donde se extraeran las imagenes
    
$array scandir($dir); // lo escaneo xd
    
$cnt sizeof($array); // lo guardo
    
echo "
            <form name='memeSelect'>
                <select name='Memes' onChange='ChangeMeme()'>
    "
;
    for(
$x=2;$x<$cnt;$x++)
    {
        
$tMemes++;
        
/* obtengo su URL: "$dir/$array[$x]" con comillas para cambiar la imagen canvas o nose... */
        
echo "
        <option id='meme_img' value='"
$dir/$array[$x]"'>Meme - $tMemes</option>
        "
;
    }
    echo 
"
                  </select>
            </form>
    "
;
?>
</p>
  <p>
    <textarea id=top>TEXTO 1</textarea>
    <textarea id=bottom>TEXTO 2</textarea>
  <br><br><br><br>
  <canvas id="meme_generado" width=508 height=379></canvas>
  </p>
  <p>
    <input type="button" name="download" value="Descargar" id="download"/>
  </p>
</center>

Última edición por flomix77; 14/04/2013 a las 11:50
  #2 (permalink)  
Antiguo 15/04/2013, 09:38
 
Fecha de Ingreso: abril-2013
Mensajes: 42
Antigüedad: 11 años
Puntos: 0
Respuesta: Cambiar imagen canvas html5 javascript?

ayudenme ya paso un dia solo me hize la cuenta por esto ayudaaaaa
  #3 (permalink)  
Antiguo 15/04/2013, 11:05
Avatar de hackjose  
Fecha de Ingreso: abril-2010
Ubicación: Edo Mexico
Mensajes: 1.178
Antigüedad: 14 años
Puntos: 131
Respuesta: Cambiar imagen canvas html5 javascript?

Para cambiar la imagen debes borrar la primera y dibujar la otra.

As algo del tipo

Cuando el valor de select cambie
Entonces borrar imagen actual con clearRect
Despues pintar nuevamente la imagen digamos con drawImage
  #4 (permalink)  
Antiguo 15/04/2013, 14:02
 
Fecha de Ingreso: abril-2013
Mensajes: 42
Antigüedad: 11 años
Puntos: 0
Respuesta: Cambiar imagen canvas html5 javascript?

Cita:
Iniciado por hackjose Ver Mensaje
Para cambiar la imagen debes borrar la primera y dibujar la otra.

As algo del tipo

Cuando el valor de select cambie
Entonces borrar imagen actual con clearRect
Despues pintar nuevamente la imagen digamos con drawImage
ooh buenisima idea lo intentare y te aviso!, Gracias.

edito: no tengo idea de como hacerlo x_x ayudame porfa

Última edición por flomix77; 15/04/2013 a las 14:09
  #5 (permalink)  
Antiguo 15/04/2013, 14:44
Avatar de hackjose  
Fecha de Ingreso: abril-2010
Ubicación: Edo Mexico
Mensajes: 1.178
Antigüedad: 14 años
Puntos: 131
Respuesta: Cambiar imagen canvas html5 javascript?

Pones el evento onchange al select

Al listener del onchange le pones este "codigo"
Borrar la imagen que se hace con clearRect(canvas.height,canvas,width);
Pinta la nueva imagen drawImage(url imagen, tamaño);

Asi quedaria, salu2
  #6 (permalink)  
Antiguo 15/04/2013, 17:27
 
Fecha de Ingreso: abril-2013
Mensajes: 42
Antigüedad: 11 años
Puntos: 0
Respuesta: Cambiar imagen canvas html5 javascript?

Cita:
Iniciado por hackjose Ver Mensaje
Pones el evento onchange al select

Al listener del onchange le pones este "codigo"
Borrar la imagen que se hace con clearRect(canvas.height,canvas,width);
Pinta la nueva imagen drawImage(url imagen, tamaño);

Asi quedaria, salu2
no hize eso .. era más simple ya lo solucione :3

Etiquetas: canvas, html5, input, php, select
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 01:38.