2017-11-23 8 views
0

こんにちは私はJavaScriptに新しいです、これは私の問題です、私はミームジェネレータを作成しようとしていますが、入力を開始します。私はイメージに添付されているようなmemeを取得したい、何が問題なのかわからない。

enter image description hereキャンバス上の画像は、入力を開始したときにのみ表示されます

また、どのようにキャンバスで画像を反応させるかわからない。 ありがとうございました。

<html> 
 
\t <head> 
 
\t \t <title>Meme Generator</title> 
 
\t \t <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
\t \t <style></style> 
 
\t \t 
 
\t \t <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> 
 
\t \t 
 
\t \t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
 
\t \t <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" /> 
 
\t \t 
 
\t </head> 
 
\t 
 
\t <body> 
 
\t 
 
<div class="container"/> 
 
    
 
    
 
</canvas> \t \t 
 
\t \t <div class="row"> 
 
     <div class="col-md-2"> 
 
\t \t <div id="thumbs"> 
 
      <img src="https://i.imgur.com/x8La5Km.jpg" class="img-responsive" alt="1st image description" /> 
 
      <img src="https://i.imgur.com/EXzUdtK.jpg" class="img-responsive" alt="2nd image description" /> 
 
      <img src="https://i.imgur.com/IJW9cPN.jpg" class="img-responsive" alt="3rd image description" /> 
 
      
 
     </div> 
 
\t \t 
 
\t \t </div> 
 
\t \t 
 
     <div class="col-md-6"> 
 
\t \t 
 
\t \t <div id="panel"> 
 
\t \t <canvas id="memecanvas"> 
 
      <img id="largeImage" src="https://i.imgur.com/x8La5Km.jpg" class="img-responsive" /> 
 
      </canvas> 
 
\t \t </div> 
 
\t \t 
 
\t \t </div> 
 
     <div class="col-md-4"> 
 
\t \t <div class="form-group"> 
 
      <input class="form-control" id="inputdefault" type="text"> 
 
      </div> 
 
\t \t <div class="form-group"> 
 
      <input class="form-control" id="inputdefault2" type="text"> 
 
      </div> 
 
\t \t <button type="button" class="btn btn-primary btn-block">CLEAR</button> 
 
\t \t <button type="button" class="btn btn-primary btn-block">SHARE ON FACEBOOK</button> 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t </div> 
 
     </div> 
 
</div> 
 
\t 
 
\t \t 
 
\t \t 
 
\t 
 
\t \t <script> 
 
\t \t $('#thumbs img').click(function(){ 
 
    $('#largeImage').attr('src',$(this).attr('src').replace('thumb','large')); 
 
    $('#description').html($(this).attr('alt')); 
 
}); 
 

 

 
    var memeSize = 600; 
 

 
    var canvas = document.getElementById('memecanvas'); 
 
    ctx = canvas.getContext('2d'); 
 

 

 
    // Set the text style to that to which we are accustomed 
 

 

 

 
    canvas.width = 600; 
 
    canvas.height = 600; 
 

 
    // Grab the nodes 
 
    var img = document.getElementById('largeImage'); 
 
    var topText = document.getElementById('inputdefault'); 
 
    var bottomText = document.getElementById('inputdefault2'); 
 

 
    // When the image has loaded... 
 
    img.onload = function() { 
 
    drawMeme() 
 
    } 
 

 
    topText.addEventListener('keydown', drawMeme) 
 
    topText.addEventListener('keyup', drawMeme) 
 
    topText.addEventListener('change', drawMeme) 
 

 
    bottomText.addEventListener('keydown', drawMeme) 
 
    bottomText.addEventListener('keyup', drawMeme) 
 
    bottomText.addEventListener('change', drawMeme) 
 

 
    function drawMeme() { 
 
    ctx.clearRect(0, 0, canvas.width, canvas.height); 
 

 
    ctx.drawImage(img, 0, 0, memeSize, memeSize); 
 

 
    ctx.lineWidth = 4; 
 
    ctx.font = '20pt sans-serif'; 
 
    ctx.strokeStyle = 'black'; 
 
    ctx.fillStyle = 'white'; 
 
    ctx.textAlign = 'center'; 
 
    ctx.textBaseline = 'top'; 
 

 
    var text1 = document.getElementById('inputdefault').value; 
 
    text1 = text1.toUpperCase(); 
 
    x = memeSize/2; 
 
    y = 0; 
 

 
    wrapText(ctx, text1, x, y, 300, 28, false); 
 

 
    ctx.textBaseline = 'bottom'; 
 
    var text2 = document.getElementById('inputdefault2').value; 
 
    text2 = text2.toUpperCase(); 
 
    y = memeSize; 
 

 
    wrapText(ctx, text2, x, y, 300, 28, true); 
 

 
    } 
 

 
    function wrapText(context, text, x, y, maxWidth, lineHeight, fromBottom) { 
 

 
    var pushMethod = (fromBottom)?'unshift':'push'; 
 
    
 
    lineHeight = (fromBottom)?-lineHeight:lineHeight; 
 

 
    var lines = []; 
 
    var y = y; 
 
    var line = ''; 
 
    var words = text.split(' '); 
 

 
    for (var n = 0; n < words.length; n++) { 
 
     var testLine = line + ' ' + words[n]; 
 
     var metrics = context.measureText(testLine); 
 
     var testWidth = metrics.width; 
 

 
     if (testWidth > maxWidth) { 
 
     lines[pushMethod](line); 
 
     line = words[n] + ' '; 
 
     } else { 
 
     line = testLine; 
 
     } 
 
    } 
 
    lines[pushMethod](line); 
 

 
    for (var k in lines) { 
 
     context.strokeText(lines[k], x, y + lineHeight * k); 
 
     context.fillText(lines[k], x, y + lineHeight * k); 
 
    } 
 

 

 
    } 
 

 

 
\t \t </script> 
 
\t </body> 
 
</html>

+2

あなたの質問にのみ関連するコードを表示するように更新してください。 –

+0

[コードを実行]をクリックします。エラーを参照し、エラーを修正します。 –

答えて

0

編集ヘッドタグでのメタデータ:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 

応答になるために:

<meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
関連する問題