2017-01-15 9 views
1

私はキャンバスにミッキーマウスの絵を作成しようとしています。 enter image description here私が管理できるすべては今、私の質問は、私はキャンバスにミッキーマウスの絵を追加することができますどのようにHTML5を使用しているJavaScriptとHTML5 javascriptを使ってhtml5でキャンバスに絵を描く方法

<html> 
 
    <canvas id="gameCanvas" width="800" height="600"></canvas> 
 

 
    <script> 
 
    function draw_bordered_rect(context, x, y, w, h) { 
 
var colors = ['grey','red','black','green','orange','purple','yellow'];  
 
context.rect(x, y, w, h); 
 
     context.fillStyle = "green"; 
 
     context.fill(); 
 

 
     context.lineWidth = 3; 
 
     context.strokeStyle = "lightblue"; 
 
     context.stroke(); 
 
canvasContext.font = '25pt Arial'; 
 
     canvasContext.textAlign = 'center'; 
 
canvasContext.fillStyle = colors[x]; 
 
    //canvasContext.fillStyle = "black"; 
 
    canvasContext.fillText('ACTIVITY 1',canvas.width/2-2, 56); 
 
     
 
    } 
 

 

 

 
    window.onload = function() { 
 
     canvas = document.getElementById('gameCanvas'); 
 
     canvasContext = canvas.getContext('2d'); 
 
     canvasContext.fillStyle = 'white'; 
 
     canvasContext.fillRect(0, 0, canvas.width, canvas.height); 
 

 
    
 

 
     draw_bordered_rect(canvasContext, 0, 0, 790, 70); 
 
     draw_bordered_rect(canvasContext, 0, 520, 790, 70); 
 
    
 
    
 

 
canvasContext.fillStyle = 'grey'; 
 
\t canvasContext.fillRect(20, 150, 40, 40); 
 
canvasContext.fillStyle = 'orange'; 
 
\t canvasContext.fillRect(20, 200, 40, 40); 
 
canvasContext.fillStyle = 'purple'; 
 
\t canvasContext.fillRect(20, 250, 40, 40); 
 
canvasContext.fillStyle = 'magenta'; 
 
\t canvasContext.fillRect(20, 300, 40, 40); 
 

 
     
 

 
canvasContext.fillStyle = 'red'; 
 
\t canvasContext.fillRect(70, 150, 40, 40); 
 
canvasContext.fillStyle = 'green'; 
 
\t canvasContext.fillRect(70, 200, 40, 40); 
 
canvasContext.fillStyle = 'blue'; 
 
\t canvasContext.fillRect(70, 250, 40, 40); 
 
canvasContext.fillStyle = 'yellow'; 
 
\t canvasContext.fillRect(70, 250, 40, 40); 
 
canvasContext.fillStyle = 'black'; 
 
\t canvasContext.fillRect(70, 300, 40, 40); 
 

 

 

 
    } 
 

 
    </script> 
 

 
</html>
を用いて、以下のでしょうか?

答えて

-1

ただ、ここでワーキングソリューションだ

base_image = new Image(); 
    base_image.src = 'pic1.png'; 
    base_image.onload = function(){ 
     canvasContext.drawImage(base_image, 100, 100); 
    } 

あなたwindow.onloadに次のコードを追加します。それが役に立てば幸い!それは美しく働く

function draw_bordered_rect(context, x, y, w, h) { 
 
     var colors = ['grey','red','black','green','orange','purple','yellow']; 
 
     context.rect(x, y, w, h); 
 
     context.fillStyle = "green"; 
 
     context.fill(); 
 

 
     context.lineWidth = 3; 
 
     context.strokeStyle = "lightblue"; 
 
     context.stroke(); 
 
     canvasContext.font = '25pt Arial'; 
 
     canvasContext.textAlign = 'center'; 
 
     canvasContext.fillStyle = colors[x]; 
 
     //canvasContext.fillStyle = "black"; 
 
     canvasContext.fillText('ACTIVITY 1',canvas.width/2-2, 56); 
 

 
    } 
 

 
    window.onload = function() { 
 
     canvas = document.getElementById('gameCanvas'); 
 
     canvasContext = canvas.getContext('2d'); 
 
     canvasContext.fillStyle = 'white'; 
 
     canvasContext.fillRect(0, 0, canvas.width, canvas.height); 
 

 
     base_image = new Image(); 
 
     base_image2 = new Image(); 
 
     base_image.src = 'https://upload.wikimedia.org/wikipedia/en/d/d4/Mickey_Mouse.png'; 
 
     base_image2.src = 'https://piratification.blob.core.windows.net/historyofpencils/picture-of-pink-pencil-eraser-small.jpg'; 
 
     base_image.onload = function(){ 
 
      canvasContext.drawImage(base_image, 100, 100); 
 
     } 
 

 
     base_image2.onload = function(){ 
 
      canvasContext.drawImage(base_image2, 400, 100); 
 
     } 
 

 

 
     draw_bordered_rect(canvasContext, 0, 0, 790, 70); 
 
     draw_bordered_rect(canvasContext, 0, 520, 790, 70); 
 

 
     canvasContext.fillStyle = 'grey'; 
 
     canvasContext.fillRect(20, 150, 40, 40); 
 
     canvasContext.fillStyle = 'orange'; 
 
     canvasContext.fillRect(20, 200, 40, 40); 
 
     canvasContext.fillStyle = 'purple'; 
 
     canvasContext.fillRect(20, 250, 40, 40); 
 
     canvasContext.fillStyle = 'magenta'; 
 
     canvasContext.fillRect(20, 300, 40, 40); 
 
     
 
     canvasContext.fillStyle = 'red'; 
 
     canvasContext.fillRect(70, 150, 40, 40); 
 
     canvasContext.fillStyle = 'green'; 
 
     canvasContext.fillRect(70, 200, 40, 40); 
 
     canvasContext.fillStyle = 'blue'; 
 
     canvasContext.fillRect(70, 250, 40, 40); 
 
     canvasContext.fillStyle = 'yellow'; 
 
     canvasContext.fillRect(70, 250, 40, 40); 
 
     canvasContext.fillStyle = 'black'; 
 
     canvasContext.fillRect(70, 300, 40, 40); 
 
    }
<canvas id="gameCanvas" width="800" height="600"></canvas>

+0

も感謝します。 –

+0

btw消しゴムの別のイメージを追加したいのですが? –

+0

あなたのコードに基づいて消しゴムの別のイメージを追加すると、ミッキーマウスのイメージは消しゴムに置き換えられます。なぜ2つの画像が共存できないのですか? –

関連する問題