LoginSignup
0
1

More than 3 years have passed since last update.

【p5.js】WebEditorで自作の画像を使用する

Last updated at Posted at 2021-03-14

お手軽で十分きれいなデザインの開発環境を調べたらp5.jsを見つけた。
往々にして日本語情報が少ないので調査ログを残していく。

開発環境

p5.js Web Editor
https://editor.p5js.org/

画像をアップロードする

  1. エディタの中段上部 sketch.js 1の右側の<をクリックしてファイルツリーを表示する
  2. Sketch Filesを右クリックしてCreateFolderから画像保存先フォルダを作成する。※フォルダは無くても問題ない。
  3. Sketch Filesを右クリックしてUploadFileから画像をアップロードする。 Screen Shot 2021-03-14 at 13.14.54.png

画像をpreloadしてから描画する



let alien;
let img;

// 画像はキャンバスのsetupより前にpreloadする
function preload(){
  img = loadImage('assets/invader1.png')
}

function setup() {
  createCanvas(400, 400);
  // 下記でAlienクラスのフィールドに画像をセットする
  alien = new Alien(50,50,img)
}

function draw() {
  background(220);
  //ロードした画像の描画、draw()メソッドはimage()を呼び出している。
  alien.draw();
}

class Alien {
    constructor(x, y, image) {
        this.x = x;
        this.y = y;
        this.image = image;
    }

    draw() {
        image(this.image, this.x, this.y, this.image.width/30, this.image.height/30);
    }
}

実行結果

Screen Shot 2021-03-14 at 13.32.02.png

まとめ

詳細は腹括って公式リファレンスを読もう。
https://p5js.org/reference/#/p5/loadImage

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1