Last active
August 29, 2015 14:19
-
-
Save remvee/d577e5f4d6a00e4637bc to your computer and use it in GitHub Desktop.
Draw image on a canvas with the given EXIF orientation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn draw-image [canvas img orientation] | |
(let [ctx (.getContext canvas "2d") | |
[width height] (.-width img) (.-height img)] | |
(case orientation | |
(1 nil) ; left top | |
(do | |
(set! (.-width canvas) width) | |
(set! (.-height canvas) height) | |
(.drawImage ctx img 0 0 width height)) | |
2 (do ; top right | |
(set! (.-width canvas) width) | |
(set! (.-height canvas) height) | |
(.scale ctx -1 1) | |
(.drawImage ctx img (* -1 width) 0 width height)) | |
3 (do ; right top | |
(set! (.-width canvas) width) | |
(set! (.-height canvas) height) | |
(.rotate ctx (.-PI js/Math)) | |
(.drawImage ctx img (* -1 width) (* -1 height) width height)) | |
4 (do ; left top | |
(set! (.-width canvas) width) | |
(set! (.-height canvas) height) | |
(.scale ctx 1 -1) | |
(.drawImage ctx img 0 (* -1 height) width height)) | |
5 (do ; top left | |
(set! (.-width canvas) height) | |
(set! (.-height canvas) width) | |
(.rotate ctx (/ (.-PI js/Math) -2)) | |
(.scale ctx -1 1) | |
(.drawImage ctx img 0 0 width height)) | |
6 (do ; top right | |
(set! (.-width canvas) height) | |
(set! (.-height canvas) width) | |
(.rotate ctx (/ (.-PI js/Math) 2)) | |
(.drawImage ctx img 0 (* -1 height) width height)) | |
7 (do ; bottom right | |
(set! (.-width canvas) height) | |
(set! (.-height canvas) width) | |
(.rotate ctx (/ (.-PI js/Math) -2)) | |
(.scale ctx 1 -1) | |
(.drawImage ctx img 0 (* -1 height) (* -1 width) height)) | |
8 (do ; bottom left | |
(set! (.-width canvas) height) | |
(set! (.-height canvas) width) | |
(.rotate ctx (/ (.-PI js/Math) -2)) | |
(.drawImage ctx img 0 0 (* -1 width) height))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment