Skip to content

Instantly share code, notes, and snippets.

@dbazile
Created July 25, 2016 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbazile/188a68fb5feae8f0023771fb60f8c25a to your computer and use it in GitHub Desktop.
Save dbazile/188a68fb5feae8f0023771fb60f8c25a to your computer and use it in GitHub Desktop.
import {CancelablePromise} from './CancelablePromise'
export function fetchThumbnail(url) {
const cancelable = new CancelablePromise((resolve, reject) => {
const thumbnail = new Image()
thumbnail.crossOrigin = 'Anonymous'
thumbnail.onload = () => resolve(thumbnail)
thumbnail.onerror = () => reject(new Error(`fetch failed (url=${url})`))
thumbnail.src = url
})
// Ensure we don't process a cancelled fetch
cancelable.promise.then(applyClippingMask)
return cancelable
}
function applyClippingMask(image) {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const context = canvas.getContext('2d')
// Set up for cleanup
context.save()
// Skew & rotate view
context.setTransform(1, 0, 0, 1, canvas.width * 0.01, canvas.height * -0.035)
context.beginPath()
context.rotate(12.25 * (Math.PI / 180))
// Draw mask
context.rect(
canvas.width * 0.178,
canvas.height * 0,
canvas.width * 0.83,
canvas.height * 0.83
)
context.clip()
// Inject image
context.setTransform(1, 0, 0, 1, 0, 0)
context.drawImage(image, 0, 0, canvas.width, canvas.height)
// Export masked image
image.src = canvas.toDataURL('image/png')
// Clean up
context.restore()
return image
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment