Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 392x 17x 17x 17x 17x 3x 1x 3x 2x 2x 2x 2x 2x 3x 17x 3x 17x 14x 14x 17x 392x 366x 366x 366x 392x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 370x 370x 370x 367x 367x 367x 370x 3x 3x 3x 370x 370x 1x 4x 4x 1x 2x 2x 1x 4x 4x 1x 2x 2x 1x 1x 3x 3x 3x 1x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 1x 22x 22x 3x 3x 3x 3x 22x 22x 3x 22x 19x 19x 19x 19x 19x 22x 1x 390x 390x 371x 371x 371x 390x 19x 19x 19x 19x 390x 390x 390x 390x 390x 390x 390x 1x 367x 1x 1x 1x 1x 1x 1x 367x 367x 1x 1x 714x 714x 363x 363x 363x 714x 714x 1x 1x 2x 2x 2x 1x 19x 19x 19x 19x 19x 19x 1x 1x 1x 5x 5x 1x 4x 4x 4x 1x 6x 6x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2048x 2048x 2048x 2048x 2048x 2048x 2048x 2x 2x 2x 1x 9x 9x 9x 9x 1x 1x 1x 1x 342x 342x 342x 342x 1x 1x 1x 24x 24x 24x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x | /* * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey * http://juerglehni.com/ & https://puckey.studio/ * * Distributed under the MIT license. See LICENSE file for details. * * All rights reserved. */ // TODO: remove eslint-disable comment and deal with errors over time /* eslint-disable */ import { ref } from '~/globals'; import { Base } from '~/straps'; import { Matrix } from '~/basic/Matrix'; import { Point } from '~/basic/Point'; import { Rectangle } from '~/basic/Rectangle'; import { Numerical } from '~/util/Numerical'; import { Change } from '~/item/ChangeFlag'; import { LinkedSize, Size } from '~/basic/Size'; import { Item } from './Item'; /** * @name Raster * * @class The Raster item represents an image in a Paper.js project. * * @extends Item */ export const Raster = Item.extend( /** @lends Raster# */ { _class: 'Raster', _applyMatrix: false, _canApplyMatrix: false, // Raster doesn't make the distinction between the different bounds, // so use the same name for all of them _boundsOptions: { stroke: false, handle: false }, _serializeFields: { crossOrigin: null, // NOTE: Needs to be set before source to work! source: null, }, // Prioritize `crossOrigin` over `source`: _prioritize: ['crossOrigin'], _smoothing: 'low', // Enforce creation of beans, as bean getters have hidden parameters. // See #getContext(_change) below. beans: true, // TODO: Implement type, width, height. // TODO: Have SymbolItem & Raster inherit from a shared class? /** * Creates a new raster item from the passed argument, and places it in the * active layer. `source` can either be a DOM Image, a Canvas, or a string * describing the URL to load the image from, or the ID of a DOM element to * get the image from (either a DOM Image or a Canvas). * * @name Raster#initialize * @param {HTMLImageElement|HTMLCanvasElement|String} [source] the source of * the raster * @param {Point} [position] the center position at which the raster item is * placed * * @example {@paperscript height=300} // Creating a raster using a url * var url = 'http://assets.paperjs.org/images/marilyn.jpg'; * var raster = new Raster(url); * * // If you create a Raster using a url, you can use the onLoad * // handler to do something once it is loaded: * raster.onLoad = function() { * console.log('The image has loaded.'); * }; * * @example // Creating a raster using the id of a DOM Image: * * // Create a raster using the id of the image: * var raster = new Raster('art'); * * @example // Creating a raster using a DOM Image: * * // Find the element using its id: * var imageElement = document.getElementById('art'); * * // Create the raster: * var raster = new Raster(imageElement); */ /** * Creates a new empty raster of the given size, and places it in the * active layer. * * @name Raster#initialize * @param {Size} size the size of the raster * @param {Point} [position] the center position at which the raster item is * placed * * @example {@paperscript height=150} * // Creating an empty raster and fill it with random pixels: * var width = 100; * var height = 100; * * // Create an empty raster placed at view center. * var raster = new Raster(new Size(width, height), view.center); * * // For all of its pixels... * for (var i = 0; i < width; i++) { * for (var j = 0; j < height; j++) { * // ...set a random color. * raster.setPixel(i, j, Color.random()); * } * } */ /** * Creates a new raster from an object description, and places it in the * active layer. * * @name Raster#initialize * @param {Object} object an object containing properties to be set on the * raster * * @example {@paperscript height=300} * var raster = new Raster({ * source: 'http://assets.paperjs.org/images/marilyn.jpg', * position: view.center * }); * * raster.scale(0.5); * raster.rotate(10); */ initialize: function Raster(source, position) { // Support three forms of item initialization: // - One object literal describing all the different properties. // - An image (Image|Canvas|String) and an optional position (Point). // - A size (Size) describing the canvas that will be created and an // optional position (Point). // If _initialize can set properties through object literal, we're done. // Otherwise we need to check the type of object: if (!this._initialize(source, position !== undefined && Point.read(arguments))) { var image, type = typeof source, object = type === 'string' ? globalThis.document.getElementById(source) : type === 'object' ? source : null; if (object && object !== Item.NO_INSERT) { if (object.getContext || object.naturalHeight != null) { image = object; } else if (object) { // See if the arguments describe the raster size: var size = Size.read(arguments); if (!size.isZero()) { // @ts-expect-error = Expected 3 arguments, but got 1 image = ref.CanvasProvider.getCanvas(size); } } } if (image) { // #setImage() handles both canvas and image types. this.setImage(image); } else { this.setSource(source); } } if (!this._size) { this._size = new Size(); this._loaded = false; } }, _equals: function (item) { return this.getSource() === item.getSource(); }, copyContent: function (source) { var image = source._image, canvas = source._canvas; if (image) { this._setImage(image); } else if (canvas) { // If the Raster contains a Canvas object, we need to create a new // one and draw this raster's canvas on it. // @ts-expect-error = Expected 3 arguments, but got 1 var copyCanvas = ref.CanvasProvider.getCanvas(source._size); copyCanvas.getContext('2d').drawImage(canvas, 0, 0); this._setImage(copyCanvas); } // TODO: Shouldn't this be copied with attributes instead of content? this._crossOrigin = source._crossOrigin; }, /** * The size of the raster in pixels. * * @bean * @type Size */ getSize: function () { var size = this._size; return new LinkedSize(size ? size.width : 0, size ? size.height : 0, this, 'setSize'); }, setSize: function (_size, _clear) { var size = Size.read(arguments); if (!size.equals(this._size)) { // NOTE: this._size could be null if (size.width > 0 && size.height > 0) { // Get reference to image before changing canvas. var element = !_clear && this.getElement(); // NOTE: Setting canvas internally sets _size. // NOTE: No need to release canvas because #_setImage() does so. // @ts-expect-error = Expected 3 arguments, but got 1 this._setImage(ref.CanvasProvider.getCanvas(size)); if (element) { // Draw element back onto the new, resized canvas. this.getContext(true).drawImage(element, 0, 0, size.width, size.height); } } else { // 0-width / height dimensions do not require the creation of // an internal canvas. Just reflect the size for now. if (this._canvas) ref.CanvasProvider.release(this._canvas); this._size = size.clone(); } } else if (_clear) { // We can reuse the canvas, but need to clear it. this.clear(); } }, /** * The width of the raster in pixels. * * @bean * @type Number */ getWidth: function () { return this._size ? this._size.width : 0; }, setWidth: function (width) { this.setSize(width, this.getHeight()); }, /** * The height of the raster in pixels. * * @bean * @type Number */ getHeight: function () { return this._size ? this._size.height : 0; }, setHeight: function (height) { this.setSize(this.getWidth(), height); }, /** * The loading state of the raster image. * * @bean * @type Boolean */ getLoaded: function () { return this._loaded; }, isEmpty: function () { var size = this._size; return !size || (size.width === 0 && size.height === 0); }, /** * The resolution of the raster at its current size, in PPI (pixels per * inch). * * @bean * @type Size */ getResolution: function () { var matrix = this._matrix, orig = new Point(0, 0).transform(matrix), u = new Point(1, 0).transform(matrix).subtract(orig), v = new Point(0, 1).transform(matrix).subtract(orig); return new Size(72 / u.getLength(), 72 / v.getLength()); }, /** * @private * @bean * @deprecated use {@link #resolution} instead. */ getPpi: '#getResolution', /** * The HTMLImageElement or Canvas element of the raster, if one is * associated. * Note that for consistency, a {@link #onLoad} event will be triggered on * the raster even if the image has already finished loading before, or if * we are setting the raster to a canvas. * * @bean * @type HTMLImageElement|HTMLCanvasElement */ getImage: function () { return this._image; }, setImage: function (image) { var that = this; function emit(event) { var view = that.getView(), type = (event && event.type) || 'load'; if (view && that.responds(type)) { ref.paper = view._scope; that.emit(type, new ref.Event(event)); } } this._setImage(image); if (this._loaded) { // Emit load event with a delay, so behavior is the same as when // it's actually loaded and we give the code time to install event. setTimeout(emit, 0); } else if (image) { // Trigger the load event on the image once it's loaded ref.DomEvent.add(image, { load: function (event) { that._setImage(image); emit(event); }, error: emit, }); } }, /** * Internal version of {@link #setImage(image)} that does not trigger * events. This is used by #setImage(), but also in other places where * underlying canvases are replaced, resized, etc. */ _setImage: function (image) { if (this._canvas) ref.CanvasProvider.release(this._canvas); // Due to similarities, we can handle both canvas and image types here. if (image && image.getContext) { // A Canvas object this._image = null; this._canvas = image; this._loaded = true; } else { // A Image object this._image = image; this._canvas = null; this._loaded = !!(image && image.src && image.complete); } // Both canvas and image have width / height attributes. Due to IE, // naturalWidth / Height needs to be checked for a swell, because it // apparently can have width / height set to 0 when the image is // invisible in the document. this._size = new Size( image ? image.naturalWidth || image.width : 0, image ? image.naturalHeight || image.height : 0 ); this._context = null; this._changed(/*#=*/ Change.GEOMETRY | Change.PIXELS); }, /** * The Canvas object of the raster. If the raster was created from an image, * accessing its canvas causes the raster to try and create one and draw the * image into it. Depending on security policies, this might fail, in which * case `null` is returned instead. * * @bean * @type HTMLCanvasElement */ getCanvas: function () { if (!this._canvas) { // @ts-expect-error = Expected 3 arguments, but got 1 var ctx = ref.CanvasProvider.getContext(this._size); // Since drawImage into canvas might fail based on security policies // wrap the call in try-catch and only set _canvas if we succeeded. try { if (this._image) ctx.drawImage(this._image, 0, 0); this._canvas = ctx.canvas; } catch (e) { ref.CanvasProvider.release(ctx); } } return this._canvas; }, // #setCanvas() is a simple alias to #setImage() setCanvas: '#setImage', /** * The Canvas 2D drawing context of the raster. * * @bean * @type CanvasRenderingContext2D */ getContext: function (_change) { if (!this._context) this._context = this.getCanvas().getContext('2d'); // Support a hidden parameter that indicates if the context will be used // to change the Raster object. We can notify such changes ahead since // they are only used afterwards for redrawing. if (_change) { // Also set _image to null since the Raster stops representing it. // NOTE: This should theoretically be in our own _changed() handler // for ChangeFlag.PIXELS, but since it's only happening in one place // this is fine: this._image = null; this._changed(/*#=*/ Change.PIXELS); } return this._context; }, setContext: function (context) { this._context = context; }, /** * The source of the raster, which can be set using a DOM Image, a Canvas, * a data url, a string describing the URL to load the image from, or the * ID of a DOM element to get the image from (either a DOM Image or a * Canvas). Reading this property will return the url of the source image or * a data-url. * Note that for consistency, a {@link #onLoad} event will be triggered on * the raster even if the image has already finished loading before. * * @bean * @type HTMLImageElement|HTMLCanvasElement|String * * @example {@paperscript} * var raster = new Raster(); * raster.source = 'http://paperjs.org/about/paper-js.gif'; * raster.position = view.center; * * @example {@paperscript} * var raster = new Raster({ * source: 'http://paperjs.org/about/paper-js.gif', * position: view.center * }); */ getSource: function () { var image = this._image; return (image && image.src) || this.toDataURL(); }, setSource: function (src) { var image = new globalThis.Image(), crossOrigin = this._crossOrigin; if (crossOrigin) image.crossOrigin = crossOrigin; // Prevent setting image source to `null`, as this isn't supported by // browsers, and it would actually throw exceptions in JSDOM. // TODO: Look into fixing this bug in JSDOM. if (src) image.src = src; this.setImage(image); }, /** * The crossOrigin value to be used when loading the image resource, in * order to support CORS. Note that this needs to be set before setting the * {@link #source} property in order to always work (e.g. when the image is * cached in the browser). * * @bean * @type String * * @example {@paperscript} * var raster = new Raster({ * crossOrigin: 'anonymous', * source: 'http://assets.paperjs.org/images/marilyn.jpg', * position: view.center * }); * * console.log(view.element.toDataURL('image/png').substring(0, 32)); */ getCrossOrigin: function () { var image = this._image; return (image && image.crossOrigin) || this._crossOrigin || ''; }, setCrossOrigin: function (crossOrigin) { this._crossOrigin = crossOrigin; var image = this._image; if (image) image.crossOrigin = crossOrigin; }, /** * Determines if the raster is drawn with pixel smoothing when scaled up or * down, and if so, at which quality its pixels are to be smoothed. The * settings of this property control both the `imageSmoothingEnabled` and * `imageSmoothingQuality` properties of the `CanvasRenderingContext2D` * interface. * * By default, smoothing is enabled at `'low'` quality. It can be set to of * `'off'` to scale the raster's pixels by repeating the nearest neighboring * pixels, or to `'low'`, `'medium'` or `'high'` to control the various * degrees of available image smoothing quality. * * For backward compatibility, it can can also be set to `false` (= `'off'`) * or `true` (= `'low'`). * * @bean * @type String * @default 'low' * @values 'low', 'medium', 'high', 'off' * * @example {@paperscript} var raster = new Raster({source: * 'http://assets.paperjs.org/images/marilyn.jpg', smoothing: 'off' * }); * raster.scale(5); */ getSmoothing: function () { return this._smoothing; }, setSmoothing: function (smoothing) { this._smoothing = typeof smoothing === 'string' ? smoothing : smoothing ? 'low' : 'off'; this._changed(/*#=*/ Change.ATTRIBUTE); }, // DOCS: document Raster#getElement getElement: function () { // Only return the internal element if the content is actually ready. return this._canvas || (this._loaded && this._image); }, }, /** @lends Raster# */ { // Explicitly deactivate the creation of beans, as we have functions here // that look like bean getters but actually read arguments. // See #getSubCanvas(), #getSubRaster(), #getSubRaster(), #getPixel(), // #getImageData() beans: false, /** * Extracts a part of the Raster's content as a sub image, and returns it as * a Canvas object. * * @param {Rectangle} rect the boundaries of the sub image in pixel * coordinates * * @return {HTMLCanvasElement} the sub image as a Canvas object */ getSubCanvas: function (/* rect */) { var rect = Rectangle.read(arguments), // @ts-expect-error = Expected 3 arguments, but got 1 ctx = ref.CanvasProvider.getContext(rect.getSize()); ctx.drawImage(this.getCanvas(), rect.x, rect.y, rect.width, rect.height, 0, 0, rect.width, rect.height); return ctx.canvas; }, /** * Extracts a part of the raster item's content as a new raster item, placed * in exactly the same place as the original content. * * @param {Rectangle} rect the boundaries of the sub raster in pixel * coordinates * * @return {Raster} the sub raster as a newly created raster item */ getSubRaster: function (/* rect */) { var rect = Rectangle.read(arguments), raster = new Raster(Item.NO_INSERT); raster._setImage(this.getSubCanvas(rect)); raster.translate(rect.getCenter().subtract(this.getSize().divide(2))); raster._matrix.prepend(this._matrix); raster.insertAbove(this); return raster; }, /** * Returns a Base 64 encoded `data:` URL representation of the raster. * * @return {String} */ toDataURL: function () { // See if the linked image is base64 encoded already, if so reuse it, // otherwise try using canvas.toDataURL() var image = this._image, src = image && image.src; if (/^data:/.test(src)) return src; var canvas = this.getCanvas(); return canvas ? canvas.toDataURL.apply(canvas, arguments) : null; }, /** * Draws an image on the raster. * * @param {CanvasImageSource} image * @param {Point} point the offset of the image as a point in pixel * coordinates */ drawImage: function (image /*, point */) { var point = Point.read(arguments, 1); this.getContext(true).drawImage(image, point.x, point.y); }, /** * Calculates the average color of the image within the given path, * rectangle or point. This can be used for creating raster image * effects. * * @param {Path|Rectangle|Point} object * @return {Color} the average color contained in the area covered by the * specified path, rectangle or point */ getAverageColor: function (object) { var bounds, path; if (!object) { bounds = this.getBounds(); } else if (object instanceof ref.PathItem) { // TODO: What if the path is smaller than 1 px? // TODO: How about rounding of bounds.size? path = object; bounds = object.getBounds(); } else if (typeof object === 'object') { if ('width' in object) { bounds = new Rectangle(object); } else if ('x' in object) { // Create a rectangle of 1px size around the specified point. bounds = new Rectangle(object.x - 0.5, object.y - 0.5, 1, 1); } } if (!bounds) return null; // Use a sample size of max 32 x 32 pixels, into which the path is // scaled as a clipping path, and then the actual image is drawn in and // sampled. var sampleSize = 32, width = Math.min(bounds.width, sampleSize), height = Math.min(bounds.height, sampleSize); // Reuse the same sample context for speed. Memory consumption is low // since it's only 32 x 32 pixels. var ctx = Raster._sampleContext; if (!ctx) { // @ts-expect-error = Expected 3 arguments, but got 1 ctx = Raster._sampleContext = ref.CanvasProvider.getContext(new Size(sampleSize)); } else { // Clear the sample canvas: ctx.clearRect(0, 0, sampleSize + 1, sampleSize + 1); } ctx.save(); // Scale the context so that the bounds ends up at the given sample size var matrix = new Matrix().scale(width / bounds.width, height / bounds.height).translate(-bounds.x, -bounds.y); matrix.applyToContext(ctx); // If a path was passed, draw it as a clipping mask: // See Project#draw() for an explanation of new Base() if (path) path.draw(ctx, new Base({ clip: true, matrices: [matrix] })); // Now draw the image clipped into it. this._matrix.applyToContext(ctx); var element = this.getElement(), size = this._size; if (element) ctx.drawImage(element, -size.width / 2, -size.height / 2); ctx.restore(); // Get pixel data from the context and calculate the average color value // from it, taking alpha into account. var pixels = ctx.getImageData(0.5, 0.5, Math.ceil(width), Math.ceil(height)).data, channels = [0, 0, 0], total = 0; for (var i = 0, l = pixels.length; i < l; i += 4) { var alpha = pixels[i + 3]; total += alpha; alpha /= 255; channels[0] += pixels[i] * alpha; channels[1] += pixels[i + 1] * alpha; channels[2] += pixels[i + 2] * alpha; } for (var i = 0; i < 3; i++) channels[i] /= total; return total ? ref.Color.read(channels) : null; }, /** * {@grouptitle Pixels} * Gets the color of a pixel in the raster. * * @name Raster#getPixel * @function * @param {Number} x the x offset of the pixel in pixel coordinates * @param {Number} y the y offset of the pixel in pixel coordinates * @return {Color} the color of the pixel */ /** * Gets the color of a pixel in the raster. * * @name Raster#getPixel * @function * @param {Point} point the offset of the pixel as a point in pixel * coordinates * @return {Color} the color of the pixel */ getPixel: function (/* point */) { var point = Point.read(arguments); var data = this.getContext().getImageData(point.x, point.y, 1, 1).data; // Alpha is separate now: return new ref.Color('rgb', [data[0] / 255, data[1] / 255, data[2] / 255], data[3] / 255); }, /** * Sets the color of the specified pixel to the specified color. * * @name Raster#setPixel * @function * @param {Number} x the x offset of the pixel in pixel coordinates * @param {Number} y the y offset of the pixel in pixel coordinates * @param {Color} color the color that the pixel will be set to */ /** * Sets the color of the specified pixel to the specified color. * * @name Raster#setPixel * @function * @param {Point} point the offset of the pixel as a point in pixel * coordinates * @param {Color} color the color that the pixel will be set to */ setPixel: function (/* point, color */) { var args = arguments, point = Point.read(args), color = ref.Color.read(args), components = color._convert('rgb'), alpha = color._alpha, ctx = this.getContext(true), imageData = ctx.createImageData(1, 1), data = imageData.data; data[0] = components[0] * 255; data[1] = components[1] * 255; data[2] = components[2] * 255; data[3] = alpha != null ? alpha * 255 : 255; ctx.putImageData(imageData, point.x, point.y); }, /** * Clears the image, if it is backed by a canvas. */ clear: function () { var size = this._size; this.getContext(true).clearRect(0, 0, size.width + 1, size.height + 1); }, // DOCS: document Raster#createImageData /** * {@grouptitle Image Data} * @param {Size} size * @return {ImageData} */ createImageData: function (/* size */) { var size = Size.read(arguments); return this.getContext().createImageData(size.width, size.height); }, // DOCS: document Raster#getImageData /** * @param {Rectangle} rect * @return {ImageData} */ getImageData: function (/* rect */) { var rect = Rectangle.read(arguments); if (rect.isEmpty()) rect = new Rectangle(this._size); return this.getContext().getImageData(rect.x, rect.y, rect.width, rect.height); }, // DOCS: document Raster#putImageData /** * @param {ImageData} data * @param {Point} point */ putImageData: function (data /*, point */) { var point = Point.read(arguments, 1); this.getContext(true).putImageData(data, point.x, point.y); }, // DOCS: document Raster#setImageData /** * @param {ImageData} data */ setImageData: function (data) { this.setSize(data); this.getContext(true).putImageData(data, 0, 0); }, /** * {@grouptitle Event Handlers} * * The event handler function to be called when the underlying image has * finished loading and is ready to be used. This is also triggered when * the image is already loaded, or when a canvas is used instead of an * image. * * @name Raster#onLoad * @property * @type ?Function * * @example * var url = 'http://assets.paperjs.org/images/marilyn.jpg'; * var raster = new Raster(url); * * // If you create a Raster using a url, you can use the onLoad * // handler to do something once it is loaded: * raster.onLoad = function() { * console.log('The image has finished loading.'); * }; * * // As with all events in paper.js, you can also use this notation instead * // to install multiple handlers: * raster.on('load', function() { * console.log('Now the image is definitely ready.'); * }); */ /** * * The event handler function to be called when there is an error loading * the underlying image. * * @name Raster#onError * @property * @type ?Function */ _getBounds: function (matrix, options) { var rect = new Rectangle(this._size).setCenter(0, 0); return matrix ? matrix._transformBounds(rect) : rect; }, _hitTestSelf: function (point) { if (this._contains(point)) { var that = this; return new ref.HitResult('pixel', that, { offset: point.add(that._size.divide(2)).round(), // Inject as Straps.js accessor, so #toString renders well too color: { get: function () { return that.getPixel(this.offset); }, }, }); } }, _draw: function (ctx, param, viewMatrix) { var element = this.getElement(); // Only draw if image is not empty (#1320). if (element && element.width > 0 && element.height > 0) { // Handle opacity for Rasters separately from the rest, since // Rasters never draw a stroke. See Item#draw(). ctx.globalAlpha = Numerical.clamp(this._opacity, 0, 1); // Call _setStyles() to make sure shadow is drawn (#1437). this._setStyles(ctx, param, viewMatrix); // `Raster#smoothing` controlls both the `imageSmoothingQuality` // and `imageSmoothingEnabled` canvas context properties: var smoothing = this._smoothing, disabled = smoothing === 'off'; ref.DomElement.setPrefixed( ctx, disabled ? 'imageSmoothingEnabled' : 'imageSmoothingQuality', disabled ? false : smoothing ); ctx.drawImage(element, -this._size.width / 2, -this._size.height / 2); } }, _canComposite: function () { return true; }, } ); ref.Raster = Raster; |