Javascript函数draw()形状到图像

时间:2017-05-04 23:14:41

标签: javascript html css canvas html5-canvas

我有一个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 个答案:

答案 0 :(得分: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);
}