1
0

ImageUtils.js 854 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
  13. canvas.width = image.width;
  14. canvas.height = image.height;
  15. var context = canvas.getContext( '2d' );
  16. if ( image instanceof ImageData ) {
  17. context.putImageData( image, 0, 0 );
  18. } else {
  19. context.drawImage( image, 0, 0, image.width, image.height );
  20. }
  21. }
  22. if ( canvas.width > 2048 || canvas.height > 2048 ) {
  23. return canvas.toDataURL( 'image/jpeg', 0.6 );
  24. } else {
  25. return canvas.toDataURL( 'image/png' );
  26. }
  27. }
  28. };
  29. export { ImageUtils };
粤ICP备19079148号