threepipe
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

script.ts 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {_testFinish, CanvasSnapshotPlugin, isWebpExportSupported, LoadingScreenPlugin, ThreeViewer} from 'threepipe'
  2. import {createSimpleButtons} from '../examples-utils/simple-bottom-buttons.js'
  3. const viewer = new ThreeViewer({
  4. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  5. msaa: true,
  6. renderScale: 'auto',
  7. plugins: [LoadingScreenPlugin],
  8. })
  9. async function init() {
  10. const snapshotPlugin = viewer.addPluginSync(new CanvasSnapshotPlugin())
  11. await viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr')
  12. await viewer.load('https://threejs.org/examples/models/gltf/kira.glb', {
  13. autoCenter: true,
  14. autoScale: true,
  15. })
  16. createSimpleButtons({
  17. ['Download snapshot (rect png)']: async(btn: HTMLButtonElement) => {
  18. btn.disabled = true
  19. await snapshotPlugin.downloadSnapshot('snapshot.png', {
  20. scale: 1, // scale the final image
  21. displayPixelRatio: 2, // render scale
  22. mimeType: 'image/png', // mime type of the image
  23. rect: { // region to take snapshot. Crop center of the canvas
  24. height: viewer.canvas.clientHeight / 2,
  25. width: viewer.canvas.clientWidth / 2,
  26. x: viewer.canvas.clientWidth / 4,
  27. y: viewer.canvas.clientHeight / 4,
  28. },
  29. })
  30. btn.disabled = false
  31. },
  32. ['Download snapshot (jpeg)']: async(btn: HTMLButtonElement) => {
  33. btn.disabled = true
  34. await snapshotPlugin.downloadSnapshot('snapshot.jpeg', {
  35. mimeType: 'image/jpeg', // mime type of the image
  36. quality: 0.9, // quality of the image (0-1) only for jpeg and webp
  37. displayPixelRatio: 2, // render scale
  38. })
  39. btn.disabled = false
  40. },
  41. ['Download snapshot (webp)']: async(btn: HTMLButtonElement) => {
  42. btn.disabled = true
  43. if (!isWebpExportSupported()) {
  44. alert('WebP export is not supported in this browser, try the latest version of chrome, firefox or edge.')
  45. btn.disabled = false
  46. return
  47. }
  48. await snapshotPlugin.downloadSnapshot('snapshot.webp', {
  49. mimeType: 'image/webp', // mime type of the image
  50. scale: 1, // scale the final image
  51. quality: 0.9, // quality of the image (0-1) only for jpeg and webp
  52. displayPixelRatio: 2, // render scale
  53. })
  54. btn.disabled = false
  55. },
  56. })
  57. }
  58. init().finally(_testFinish)