image.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. /*!
  3. * Canvas - Image
  4. * Copyright (c) 2010 LearnBoost <tj@learnboost.com>
  5. * MIT Licensed
  6. */
  7. /**
  8. * Module dependencies.
  9. */
  10. var Canvas = require('./bindings')
  11. , Image = Canvas.Image;
  12. /**
  13. * Src setter.
  14. *
  15. * - convert data uri to `Buffer`
  16. *
  17. * @param {String|Buffer} val filename, buffer, data uri
  18. * @api public
  19. */
  20. Image.prototype.__defineSetter__('src', function(val){
  21. if ('string' == typeof val && 0 == val.indexOf('data:')) {
  22. val = val.slice(val.indexOf(',') + 1);
  23. this.source = new Buffer(val, 'base64');
  24. } else {
  25. this.source = val;
  26. }
  27. });
  28. /**
  29. * Src getter.
  30. *
  31. * TODO: return buffer
  32. *
  33. * @api public
  34. */
  35. Image.prototype.__defineGetter__('src', function(){
  36. return this.source;
  37. });
  38. /**
  39. * Inspect image.
  40. *
  41. * TODO: indicate that the .src was a buffer, data uri etc
  42. *
  43. * @return {String}
  44. * @api public
  45. */
  46. Image.prototype.inspect = function(){
  47. return '[Image'
  48. + (this.complete ? ':' + this.width + 'x' + this.height : '')
  49. + (this.src ? ' ' + this.src : '')
  50. + (this.complete ? ' complete' : '')
  51. + ']';
  52. };