tap-place.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (c) 2021 8th Wall, Inc.
  2. /* globals AFRAME */
  3. // Component that places trees where the ground is clicked
  4. AFRAME.registerComponent('tap-place', {
  5. init() {
  6. const ground = document.getElementById('ground')
  7. ground.addEventListener('click', (event) => {
  8. // Create new entity for the new object
  9. const newElement = document.createElement('a-entity')
  10. // The raycaster gives a location of the touch in the scene
  11. const touchPoint = event.detail.intersection.point
  12. newElement.setAttribute('position', touchPoint)
  13. const randomYRotation = Math.random() * 360
  14. newElement.setAttribute('rotation', `0 ${randomYRotation} 0`)
  15. newElement.setAttribute('visible', 'false')
  16. newElement.setAttribute('scale', '0.0001 0.0001 0.0001')
  17. newElement.setAttribute('shadow', {
  18. receive: false,
  19. })
  20. newElement.setAttribute('gltf-model', '#treeModel')
  21. this.el.sceneEl.appendChild(newElement)
  22. newElement.addEventListener('model-loaded', () => {
  23. // Once the model is loaded, we are ready to show it popping in using an animation
  24. newElement.setAttribute('visible', 'true')
  25. newElement.setAttribute('animation', {
  26. property: 'scale',
  27. to: '7 7 7',
  28. easing: 'easeOutElastic',
  29. dur: 800,
  30. })
  31. })
  32. })
  33. },
  34. })