File tree Expand file tree Collapse file tree 4 files changed +54
-2
lines changed
Expand file tree Collapse file tree 4 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -188,6 +188,10 @@ Setting the `aspectRatio` also enables the `crop` option.
188188pixels on the screen.
189189Should be set to ` window.devicePixelRatio ` unless the scaled image is not
190190rendered on screen.
191+ Defaults to ` 1 ` and requires ` canvas: true ` .
192+ * ** downsamplingRatio** : Defines the ratio in which the image is downsampled.
193+ By default, images are downsampled in one step. With a ratio of ` 0.5 ` , each step
194+ scales the image to half the size, before reaching the target dimensions.
191195Requires ` canvas: true ` .
192196* ** crop** : Crops the image to the maxWidth/maxHeight constraints if set to
193197` true ` .
Original file line number Diff line number Diff line change @@ -78,7 +78,8 @@ $(function () {
7878 var options = {
7979 maxWidth : result . width ( ) ,
8080 canvas : true ,
81- pixelRatio : window . devicePixelRatio
81+ pixelRatio : window . devicePixelRatio ,
82+ downsamplingRatio : 0.5
8283 }
8384 if ( ! file ) {
8485 return
@@ -139,7 +140,8 @@ $(function () {
139140 sourceHeight : coordinates . h * pixelRatio ,
140141 minWidth : result . width ( ) ,
141142 maxWidth : result . width ( ) ,
142- pixelRatio : pixelRatio
143+ pixelRatio : pixelRatio ,
144+ downsamplingRatio : 0.5
143145 } ) )
144146 coordinates = null
145147 }
Original file line number Diff line number Diff line change 166166 var sourceX
167167 var sourceY
168168 var pixelRatio
169+ var downsamplingRatio
169170 var tmp
170171 function scaleUp ( ) {
171172 var scale = Math . max (
251252 destHeight *= pixelRatio
252253 canvas . getContext ( '2d' ) . scale ( pixelRatio , pixelRatio )
253254 }
255+ downsamplingRatio = options . downsamplingRatio
256+ if ( downsamplingRatio > 0 && downsamplingRatio < 1 &&
257+ destWidth < sourceWidth && destHeight < sourceHeight ) {
258+ while ( sourceWidth * downsamplingRatio > destWidth ) {
259+ canvas . width = sourceWidth * downsamplingRatio
260+ canvas . height = sourceHeight * downsamplingRatio
261+ loadImage . renderImageToCanvas (
262+ canvas ,
263+ img ,
264+ sourceX ,
265+ sourceY ,
266+ sourceWidth ,
267+ sourceHeight ,
268+ 0 ,
269+ 0 ,
270+ canvas . width ,
271+ canvas . height
272+ )
273+ sourceWidth = canvas . width
274+ sourceHeight = canvas . height
275+ img = document . createElement ( 'canvas' )
276+ img . width = sourceWidth
277+ img . height = sourceHeight
278+ loadImage . renderImageToCanvas (
279+ img ,
280+ canvas ,
281+ 0 ,
282+ 0 ,
283+ sourceWidth ,
284+ sourceHeight ,
285+ 0 ,
286+ 0 ,
287+ sourceWidth ,
288+ sourceHeight
289+ )
290+ }
291+ }
254292 canvas . width = destWidth
255293 canvas . height = destHeight
256294 loadImage . transformCoordinates (
Original file line number Diff line number Diff line change 198198 } , { maxWidth : 40 , canvas : true , pixelRatio : 2 } ) ) . to . be . ok ( )
199199 } )
200200
201+ it ( 'Scale down with the given downsamplingRatio' , function ( done ) {
202+ expect ( loadImage ( blobGIF , function ( img ) {
203+ expect ( img . width ) . to . be ( 20 )
204+ expect ( img . height ) . to . be ( 15 )
205+ done ( )
206+ } , { maxWidth : 20 , canvas : true , downsamplingRatio : 0.5 } ) ) . to . be . ok ( )
207+ } )
208+
201209 it ( 'Ignore max settings if image dimensions are smaller' , function ( done ) {
202210 expect ( loadImage ( blobGIF , function ( img ) {
203211 expect ( img . width ) . to . be ( 80 )
You can’t perform that action at this time.
0 commit comments