2017-05-04 2 views
1

に形状:Javascriptの関数draw()私は音楽再生の音量に基づいて楕円とその振幅を描くjsのスクリプトを持っている画像

var song, analyzer; 

function preload() { 
    song = loadSound('sounds/masterflash.mp3'); 
} 

function setup() { 
    createCanvas(250, 250); 
    song.loop(); 

    // create a new Amplitude analyzer 
    analyzer = new p5.Amplitude(); 

    // Patch the input to an volume analyzer 
    analyzer.setInput(song); 
} 

function draw() { 
    background(0, 0, 0, 0) 

    // Get the average (root mean square) amplitude 
    var rms = analyzer.getLevel(); 
    fill(0, 0, 0, 20); 
    stroke(255, 255, 255); 

    // Draw an ellipse with size based on volume 
    ellipse(width/3, height/3, 10+rms*200, 10+rms*200); 
} 

あなたはそれがhttps://p5js.org/examples/sound-measuring-amplitude.htmlここで何をするかのサンプルを見ることができます

最後のコードで、楕円が作成されていることがわかります。どのように私は同じことをすることができますが、楕円を描く代わりに、私は持っている丸い.png画像を読み込みますか?

答えて

1

最初にpreload Imageにしてから描画します。 image()

についての詳細は、この

function preload() { 
    img = loadImage('images/laDefense.jpg'); 
    song = loadSound('sounds/masterflash.mp3'); 
} 
... 

function draw() { 
    background(0, 0, 0, 0) 

    // Get the average (root mean square) amplitude 
    var rms = analyzer.getLevel(); 
    fill(0, 0, 0, 20); 
    stroke(255, 255, 255); 

    image(img, width/3, height/3, 10+rms*200, 10+rms*200); 
} 
+0

のように試してみてくださいそれは働いていたが、今のアニメーションではなく、中央の左上隅を中心としています。あなたは再生ボタンの上でそれを見ることができますvtxfactory.org – Rui

+0

@Rui特にそのウェブサイト上のあなたの問題を見つけることができませんでしたが、とにかくイメージの位置を調整することができます。参考までにそれを行う方法を見てください。https://p5js.org/reference/#/p5/image –

関連する問題