2017-08-01 108 views
0

我想要使用Canvas/jQuery將圖像和文本放在背景圖像上,但我不知道如何做到這一點。我需要這樣做http://static.catfly.com/quiz/which-friend-should-be-kidnapted-by-aliens/en/cover.jpg如何使用Canvas或jQuery將圖像和文本放在背景圖像上?

我已經嘗試過,發現一些腳本的博客,但它不是完整的腳本,它只是一個圈子,任何人都可以幫助我嗎?

以下是我的腳本。

<body onload="displayImage()"> 

<script type="text/javascript"> 

    //Global variables 
    var myImage = new Image(); // Create a new blank image. 

    // Load the image and display it. 
    function displayImage() 
    { 
    // Get the canvas element. 
    canvas = document.getElementById("myCanvas"); 

     // Make sure you got it. 
     if (canvas.getContext) 
     { 
      // Specify 2d canvas type. 
      ctx = canvas.getContext("2d"); 

      // When the image is loaded, draw it. 
      myImage.onload = function() { 

      // Load the image into the context. 
      ctx.drawImage(myImage, 0, 0); 

      // Get and modify the image data. 
      changeImage(); 
      } 

      // Define the source of the image. 
      myImage.src = "https://pbs.twimg.com/profile_image/843519123711803393/pyYe9LFq_400x400.jpg"; 
     } 
    } 

    function changeImage() 
    { 
    ctx.strokeStyle = "white"; 
    ctx.lineWidth = "100"; 
    ctx.beginPath(); 
    ctx.arc(100, 100, 150, 0, Math.PI * 2, true); 
    ctx.closePath(); 
    ctx.stroke(); 
    } 
</script> 

<canvas id="myCanvas" width="200" height="200"></canvas> 

</body> 

回答

1

其中的一個問題是,在畫布上只有200x200和圓的線寬爲100,白色的,所以得出當它填充大部分畫布(它也略有畫布區外畫這個案例)。

而且,有問題的圖像也有一個負載錯誤,這可能是此處的主要原因(應始終添加onerror + onabort處理程序)。

只需調整線寬,固定圖像,並且我還建議將畫布設置得更大(可以使用圖像大小進行設置)。

+0

我沒有要求解決包含的腳本,我想讓這個圖像看起來像這樣http://static.catfly.com/quiz/which-friend-should-be-kidnapted-by-aliens/en/ cover.jpg –