javascript 尝试添加背景图像到突破游戏

z4iuyo4d  于 2023-02-28  发布在  Java
关注(0)|答案(2)|浏览(126)

我试图添加一个背景图像,我的老师试图解释该怎么做,但它仍然不会工作。

<!DOCTYPE html>
<html>

<head>
  <style>
    background-image: url("https://imgur.com/sJTAxQG");
  </style>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>group project</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
   <audio id="myAudio">
    <source src="8d82b5_Star_Wars_Main_Theme_Song.mp3" type="audio/mpeg">
      </audio>
  <canvas id="myCanvas" width="480" height="320"></canvas>
  <script>
   
    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");
    var background = new Image();
    background.onload = function(){
    background.src = 'https://imgur.com/sJTAxQG';
    ctx.drawImage(background,0,0);

每次我试着运行它,什么都没发生

kr98yfug

kr98yfug1#

尝试在样式中添加选择器(例如body)。

<style>
  body {
    background-image: url("https://imgur.com/sJTAxQG");
  }
</style>
y0u0uwnf

y0u0uwnf2#

这是因为您请求的资源https://imgur.com/sJTAxQG是一个HTML页面,而不是原始图像(内容类型是text/html),并且只有原始图像可以加载<img>标签或在CSS中用作background-image
要获取原始图像,请使用此链接:https://i.imgur.com/sJTAxQG.jpeg(我在网站使用它来显示图像的检查菜单中找到了这个链接)

相关问题