Javascript函数draw()形状到图像
我有一个js脚本,它根据音乐播放的音量绘制椭圆及其振幅:Javascript函数draw()形状到图像,javascript,html,css,canvas,html5-canvas,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();
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);
}
你可以在这里看到它的功能示例
您可以在最后一段代码中看到它创建了椭圆。我如何做同样的事情,但不是绘制椭圆,而是加载一个圆形的.png图像呢?你必须先绘制,然后再绘制。更多关于
像这样试试
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);
}
它工作了,但是现在动画集中在左上角而不是中间。您可以在这里的播放按钮VTX Factory上看到它。org@Rui我在那个网站上找不到你们的问题,但无论如何你们可以调整图片的位置。只要看看参考资料就知道怎么做了