diff --git a/frontend/src/app/rasterizer.cljs b/frontend/src/app/rasterizer.cljs index 81f240d0a..fcf5de377 100644 --- a/frontend/src/app/rasterizer.cljs +++ b/frontend/src/app/rasterizer.cljs @@ -192,8 +192,8 @@ (rx/map wapi/create-uri) (rx/mapcat (fn [uri] (->> (create-image uri) - (rx/mapcat #(wapi/create-image-bitmap % #js {:resizeWidth width - :resizeQuality quality})) + (rx/mapcat #(wapi/create-image-bitmap-with-workaround % #js {:resizeWidth width + :resizeQuality quality})) (rx/tap #(wapi/revoke-uri uri)))))))) (defn- render-blob diff --git a/frontend/src/app/util/webapi.cljs b/frontend/src/app/util/webapi.cljs index 9be8d1737..039f287ac 100644 --- a/frontend/src/app/util/webapi.cljs +++ b/frontend/src/app/util/webapi.cljs @@ -10,9 +10,7 @@ [app.common.data :as d] [app.common.exceptions :as ex] [app.common.logging :as log] - [app.config :as cf] [app.util.object :as obj] - [app.util.thumbnails :as th] [beicon.core :as rx] [cuerdas.core :as str] [promesa.core :as p])) @@ -57,6 +55,14 @@ ([content mtype] (js/Blob. #js [content] #js {:type mtype}))) +(defn create-blob-from-canvas + ([canvas] + (create-blob-from-canvas canvas nil)) + ([canvas options] + (if (obj/in? canvas "convertToBlob") + (.convertToBlob canvas options) + (p/create (fn [resolve _] (.toBlob #(resolve %) canvas options)))))) + (defn revoke-uri [url] (when ^boolean (str/starts-with? url "blob:") @@ -154,15 +160,19 @@ (js/createImageBitmap image options))) (defn create-image - [src width height] - (p/create - (fn [resolve reject] - (let [img (.createElement js/document "img")] - (obj/set! img "width" width) - (obj/set! img "height" height) - (obj/set! img "src" src) - (obj/set! img "onload" #(resolve img)) - (obj/set! img "onerror" reject))))) + ([src] + (create-image src nil nil)) + ([src width height] + (p/create + (fn [resolve reject] + (let [img (.createElement js/document "img")] + (when-not (nil? width) + (obj/set! img "width" width)) + (when-not (nil? height) + (obj/set! img "height" height)) + (obj/set! img "src" src) + (obj/set! img "onload" #(resolve img)) + (obj/set! img "onerror" reject)))))) ;; Why this? Because as described in https://bugs.chromium.org/p/chromium/issues/detail?id=1463435 ;; the createImageBitmap seems to apply premultiplied alpha multiples times on the same image @@ -171,23 +181,10 @@ ([image] (create-image-bitmap-with-workaround image nil)) ([^js image options] - (let [width (.-value (.-baseVal (.-width image))) - height (.-value (.-baseVal (.-height image))) - [width height] (th/get-proportional-size width height) - - image-source - (if (cf/check-browser? :safari) - (let [src (.-baseVal (.-href image))] - (create-image src width height)) - (p/resolved image))] - - (-> image-source - (p/then - (fn [html-img] - (let [offscreen-canvas (create-offscreen-canvas width height) - offscreen-context (.getContext offscreen-canvas "2d")] - (.drawImage offscreen-context html-img 0 0) - (create-image-bitmap offscreen-canvas options)))))))) + (let [offscreen-canvas (create-offscreen-canvas (.-width image) (.-height image)) + offscreen-context (.getContext offscreen-canvas "2d")] + (.drawImage offscreen-context image 0 0) + (create-image-bitmap offscreen-canvas options)))) (defn request-fullscreen [el]