Scale a rotated image to fill an HTML5 canvas using JavaScript trigonometry?

Below is the code I'm currently using. When rotating, the 0image scales properly to fill the canvas. (similarly background-size: cover, except for using JavaScript on the canvas)

I am trying to add a rotation function with the following functions.

  • Maintain a centered image while rotating. I tried using width / 2in translate, and then vice versa in drawImage, but as you can see, the image did not remain centered. I'm not sure if this is a conflict with my earlier xand ycentering code, or if trigonometry is required here?

  • Automatically scale the image further to close the canvas. These are arbitrary rotations, not 90 degrees. I don’t want to crop the image more than I need to fill the corners of the canvas. This is a much more complex trigonometry than I'm used to dealing with.

I think this can be done with Math.sinand Math.cos, but it has been a very long time since I used them. I especially doubt how to reach No. 2. Any help would be greatly appreciated.

var canvas = document.querySelector('canvas')
var context = canvas.getContext('2d')
var image = new Image()
image.src = 'http://i.stack.imgur.com/7FsbT.jpg'

image.onload = function () {
  
  var maxScaleX = canvas.width / image.width
  var maxScaleY = canvas.height / image.height
  scale = maxScaleX > maxScaleY ? maxScaleX : maxScaleY
  
  var width = image.width * scale
  var height = image.height * scale
  var x = (width - canvas.width) / -2
  var y = (height - canvas.height) / -2
  
  var degrees = 30
  
  context.setTransform(1, 0, 0, 1, 0, 0) // reset previous translate and/or rotate
  context.translate(width / 2, height / 2)
  context.rotate(degrees * Math.PI / 180)
  
  context.drawImage(image, x - width / 2, y - height / 2, width, height)

}
<canvas width="350" height="150" style="border: solid 1px black"></canvas>
Run code
+1
source share
1 answer

+1 for Blindman67 answer for this to work.

. function drawBestFit() .

, , Blindman67 - .

var canvas = document.querySelector('canvas')
var context = canvas.getContext('2d')
var image = new Image()
image.src = 'http://i.stack.imgur.com/7FsbT.jpg'

image.onload = function() {

  var degrees = 0

  loop()

  function loop() {
    degrees += .5
    drawBestFit(context, degrees * Math.PI / 180, image)
    requestAnimationFrame(loop)
  }

  function drawBestFit(ctx, angle, image) {
    
    var dist = Math.sqrt(Math.pow(canvas.width, 2) + Math.pow(canvas.height, 2))
    var diagAngle = Math.asin(canvas.height / dist)

    var a1 = ((angle % (Math.PI * 2)) + Math.PI * 4) % (Math.PI * 2)
    if (a1 > Math.PI)
      a1 -= Math.PI
    if (a1 > Math.PI / 2 && a1 <= Math.PI)
      a1 = (Math.PI / 2) - (a1 - (Math.PI / 2))
    
    var ang1 = Math.PI / 2 - diagAngle - Math.abs(a1)
    var ang2 = Math.abs(diagAngle - Math.abs(a1))
    
    var scale1 = Math.cos(ang1) * dist / image.height
    var scale2 = Math.cos(ang2) * dist / image.width
    
    var scale = Math.max(scale1, scale2)
    
    var dx = Math.cos(angle) * scale
    var dy = Math.sin(angle) * scale
    ctx.setTransform(dx, dy, -dy, dx, canvas.width / 2, canvas.height / 2)
    
    ctx.drawImage(image, -image.width / 2, -image.height / 2, image.width, image.height)
    
    ctx.setTransform(1, 0, 0, 1, 0, 0) // reset transformations when done

  }

}
<canvas width="350" height="200" style="border: solid 1px black"></canvas>
+3

Source: https://habr.com/ru/post/1627844/


All Articles