collage-task1

Benchmark created by Max Mathieu on


Setup

function sizeimage_max(canvasWidth, canvasHeight, imageAspect)
  {
      var canvasAspect = canvasWidth / canvasHeight;
      var xpos = 0, ypos = 0;
      var sizex, sizey;
      if (canvasAspect < imageAspect) {
          sizey = canvasHeight;
          sizex = canvasHeight * imageAspect;
          xpos = (canvasWidth-sizex)/2;
      } else {
          sizex = canvasWidth;
          sizey = canvasWidth / imageAspect;
          ypos = (canvasHeight -sizey)/2;
      }
      //final image position and size
      return [xpos, ypos, sizex, sizey];
  }
  
  
  function sizeimage_candidate(canvasWidth, canvasHeight, imageAspect)
  {
  	let xpos = 0, ypos = 0, sizex = 0, sizey = 0;
  	// console.log('Input:', canvasWidth, canvasHeight, imageAspect);
  
  	let height = canvasHeight,
  		width = canvasWidth,
  		aspect = width / height;
  
  	// console.log(aspect);
  	if (aspect !== imageAspect) {
  		let x = imageAspect / aspect;
  		if (x > 1) {
  			width = Math.floor(width * x);
  		} else {
  			height = Math.floor(height / x);
  		}
  		aspect = width / height;
  		// console.log(x, width, height, aspect);
  	}
  
  	sizex = width;
  	if (width > canvasWidth) {
  		xpos -= Math.floor((width - canvasWidth) / 2);
  	}
  	sizey = height;
  	if (height > canvasHeight) {
  		ypos -= Math.floor((height - canvasHeight) / 2);
  	}
  
      //final image position and size
      return [xpos, ypos, sizex, sizey];
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Max
sizeimage_max(200, 200, 1);
sizeimage_max(100, 200, 1);
sizeimage_max(200, 100, 1);
sizeimage_max(200, 200, .5);
sizeimage_max(100, 200, .5);
sizeimage_max(200, 100, .5);
sizeimage_max(100, 400, .5);
sizeimage_max(200, 200, 2);
sizeimage_max(100, 200, 2);
sizeimage_max(200, 100, 2);
sizeimage_max(400, 100, 2);
ready
candidate
sizeimage_candidate(200, 200, 1);
sizeimage_candidate(100, 200, 1);
sizeimage_candidate(200, 100, 1);
sizeimage_candidate(200, 200, .5);
sizeimage_candidate(100, 200, .5);
sizeimage_candidate(200, 100, .5);
sizeimage_candidate(100, 400, .5);
sizeimage_candidate(200, 200, 2);
sizeimage_candidate(100, 200, 2);
sizeimage_candidate(200, 100, 2);
sizeimage_candidate(400, 100, 2);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Max Mathieu on