但我把mounted改成created的时候会报Cannot read property 'appendChild' of null"
这是因为created的时候,Dom节点还没有渲染出来到页面上,这个时候是找不到id=wave的DIV的。
改成Vue的插件
import wavePng from './wave.png'
export default {
install(Vue){
Vue.directive('wave', {
inserted: function(el){
start(el)
}
})
}
}
var ctx;
var waveImage;
var canvasWidth;
var canvasHeight;
var needAnimate = false;
function init (callback, wave) {
// var wave = document.getElementById('wave');
var canvas = document.createElement('canvas');
if (!canvas.getContext) return;
ctx = canvas.getContext('2d');
canvasWidth = wave.offsetWidth;
canvasHeight = wave.offsetHeight;
canv