1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 12:22:03 +08:00
trois/src/core/Renderer.vue

71 lines
1.1 KiB
Vue
Raw Normal View History

2020-09-14 22:57:11 +08:00
<template>
<canvas ref="canvas">
<slot></slot>
</canvas>
</template>
<script>
import useThree from './useThree';
const animateCallbacks = [];
export function useAnimate(callback) {
animateCallbacks.push(callback);
};
export default {
props: {
alpha: {
type: Boolean,
default: false,
},
animate: {
type: Function,
},
},
data() {
return {
raf: true,
};
},
setup(props) {
return {
three: useThree(),
};
},
provide() {
return {
three: this.three,
};
},
mounted() {
// console.log('Renderer mounted');
this.three.init({
canvas: this.$refs.canvas,
});
this._animate();
},
beforeUnmount() {
// console.log('Renderer beforeUnmount');
// animateCallbacks.splice(0);
this.raf = false;
this.three.dispose();
},
methods: {
_animate() {
if (this.raf) requestAnimationFrame(this._animate);
// if (this.animate) this.animate();
animateCallbacks.forEach(c => c());
if (this.three.scene) this.three.render(this.three.scene);
},
},
};
</script>
<style>
canvas {
display: block;
}
</style>