ImageUtils.js 992 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author szimek / https://github.com/szimek/
  5. */
  6. var ImageUtils = {
  7. getDataURL: function ( image ) {
  8. var canvas;
  9. if ( image instanceof HTMLCanvasElement ) {
  10. canvas = image;
  11. } else {
  12. if ( typeof OffscreenCanvas !== 'undefined' ) {
  13. canvas = new OffscreenCanvas( image.width, image.height );
  14. } else {
  15. canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
  16. canvas.width = image.width;
  17. canvas.height = image.height;
  18. }
  19. var context = canvas.getContext( '2d' );
  20. if ( image instanceof ImageData ) {
  21. context.putImageData( image, 0, 0 );
  22. } else {
  23. context.drawImage( image, 0, 0, image.width, image.height );
  24. }
  25. }
  26. if ( canvas.width > 2048 || canvas.height > 2048 ) {
  27. return canvas.toDataURL( 'image/jpeg', 0.6 );
  28. } else {
  29. return canvas.toDataURL( 'image/png' );
  30. }
  31. }
  32. };
  33. export { ImageUtils };
粤ICP备19079148号