最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to load image and make it as background in p5.js - Stack Overflow

programmeradmin3浏览0评论

I just want to ask a simple question, how do I load an image and make it as a background in my html5 canvas

var bg;

function setup() {
  createCanvas(600,400)
  bg = loadImage(".jpg")
}

function draw() {
  background(bg)
}

i tried that method but the screen only showing whitescreen, i've search for it but i can't get the solution.

I just want to ask a simple question, how do I load an image and make it as a background in my html5 canvas

var bg;

function setup() {
  createCanvas(600,400)
  bg = loadImage("https://mcdn.wallpapersafari./medium/1/92/T1iecJ.jpg")
}

function draw() {
  background(bg)
}

i tried that method but the screen only showing whitescreen, i've search for it but i can't get the solution.

Share Improve this question edited Nov 9, 2019 at 8:50 Rabbid76 212k30 gold badges159 silver badges200 bronze badges asked Nov 9, 2019 at 7:21 Deven ValistenDeven Valisten 671 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

loadImage doesn't load an image synchronously. The image may not be immediately available for rendering.

Use the preload() function, to ensure that the image is load before draw() is executed. setup will wait until any load calls within preload() have finished.

var bg;

function preload(){
    bg = loadImage("https://raw.githubusercontent./Rabbid76/graphics-snippets/master/resource/texture/background.jpg")
}

function setup() {
    createCanvas(600,400)
}

function draw() {
    background(bg)
}
<script src="https://cdnjs.cloudflare./ajax/libs/p5.js/0.9.0/p5.js"></script>

发布评论

评论列表(0)

  1. 暂无评论