X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2FPixelSampler.js;h=d45ab8a30e681fcb1a8d0a0626de8af2b29a08f4;hb=refs%2Fheads%2Fopt-size-check;hp=89c520fad0bc04e97cd1fbd8b1f406beb6ded06e;hpb=9f636500f08868182ecd88288636e7f8718e28de;p=dygraphs.git diff --git a/auto_tests/tests/PixelSampler.js b/auto_tests/tests/PixelSampler.js index 89c520f..d45ab8a 100644 --- a/auto_tests/tests/PixelSampler.js +++ b/auto_tests/tests/PixelSampler.js @@ -17,6 +17,7 @@ var PixelSampler = function(dygraph) { var canvas = dygraph.hidden_; var ctx = canvas.getContext("2d"); this.imageData_ = ctx.getImageData(0, 0, canvas.width, canvas.height); + this.scale = canvas.width / dygraph.width_; }; /** @@ -26,12 +27,23 @@ var PixelSampler = function(dygraph) { * are in [0, 255]. A pixel which has never been touched will be [0,0,0,0]. */ PixelSampler.prototype.colorAtPixel = function(x, y) { - var i = 4 * (x + this.imageData_.width * y); + var i = 4 * (x * this.scale + this.imageData_.width * y * this.scale); var d = this.imageData_.data; return [d[i], d[i+1], d[i+2], d[i+3]]; }; /** + * Convenience wrapper around colorAtPixel if you only care about RGB (not A). + * @param {number} x The screen x-coordinate at which to sample. + * @param {number} y The screen y-coordinate at which to sample. + * @return {Array.} a 3D array: [R, G, B]. All three values + * are in [0, 255]. A pixel which has never been touched will be [0,0,0]. + */ +PixelSampler.prototype.rgbAtPixel = function(x, y) { + return this.colorAtPixel(x, y).slice(0, 3); +}; + +/** * The method samples a color using data coordinates (not screen coordinates). * This will round your data coordinates to the nearest screen pixel before * sampling. @@ -44,3 +56,5 @@ PixelSampler.prototype.colorAtCoordinate = function(x, y) { var dom_xy = this.dygraph_.toDomCoords(x, y); return this.colorAtPixel(Math.round(dom_xy[0]), Math.round(dom_xy[1])); }; + +export default PixelSampler;