0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 14:12:36 -05:00

Merge pull request #5459 from penpot/ladybenko-9516-fix-blend

🐛 Fix blend mode against background
This commit is contained in:
Alejandro 2024-12-12 09:38:34 +01:00 committed by GitHub
commit 953c95315a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 34 additions and 2 deletions

View file

@ -284,7 +284,8 @@
(p/fmap (fn [ready?] (p/fmap (fn [ready?]
(when ready? (when ready?
(reset! canvas-init? true) (reset! canvas-init? true)
(wasm.api/assign-canvas canvas))))) (wasm.api/assign-canvas canvas)
(wasm.api/set-canvas-background background)))))
(fn [] (fn []
(wasm.api/clear-canvas)))) (wasm.api/clear-canvas))))
@ -304,6 +305,10 @@
(when @canvas-init? (when @canvas-init?
(wasm.api/set-view zoom vbox))) (wasm.api/set-view zoom vbox)))
(mf/with-effect [background]
(when @canvas-init?
(wasm.api/set-canvas-background background)))
(hooks/setup-dom-events zoom disable-paste in-viewport? read-only? drawing-tool drawing-path?) (hooks/setup-dom-events zoom disable-paste in-viewport? read-only? drawing-tool drawing-path?)
(hooks/setup-viewport-size vport viewport-ref) (hooks/setup-viewport-size vport viewport-ref)
(hooks/setup-cursor cursor alt? mod? space? panning drawing-tool drawing-path? node-editing? z? read-only?) (hooks/setup-cursor cursor alt? mod? space? panning drawing-tool drawing-path? node-editing? z? read-only?)

View file

@ -345,6 +345,11 @@
(set! (.-width canvas) (* dpr (.-clientWidth ^js canvas))) (set! (.-width canvas) (* dpr (.-clientWidth ^js canvas)))
(set! (.-height canvas) (* dpr (.-clientHeight ^js canvas)))) (set! (.-height canvas) (* dpr (.-clientHeight ^js canvas))))
(defn set-canvas-background
[background]
(let [rgba (rgba-from-hex background 1)]
(h/call internal-module "_set_canvas_background" rgba)))
(defonce module (defonce module
(delay (delay
(if (exists? js/dynamicImport) (if (exists? js/dynamicImport)

View file

@ -49,6 +49,14 @@ pub extern "C" fn set_render_options(debug: u32, dpr: f32) {
render_state.set_dpr(dpr); render_state.set_dpr(dpr);
} }
#[no_mangle]
pub extern "C" fn set_canvas_background(raw_color: u32) {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
let color = skia::Color::new(raw_color);
state.set_background_color(color);
}
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn render() { pub unsafe extern "C" fn render() {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");

View file

@ -49,6 +49,7 @@ pub(crate) struct RenderState {
options: RenderOptions, options: RenderOptions,
pub viewbox: Viewbox, pub viewbox: Viewbox,
images: ImageStore, images: ImageStore,
background_color: skia::Color,
} }
impl RenderState { impl RenderState {
@ -72,6 +73,7 @@ impl RenderState {
options: RenderOptions::default(), options: RenderOptions::default(),
viewbox: Viewbox::new(width as f32, height as f32), viewbox: Viewbox::new(width as f32, height as f32),
images: ImageStore::new(), images: ImageStore::new(),
background_color: skia::Color::TRANSPARENT,
} }
} }
@ -97,6 +99,11 @@ impl RenderState {
} }
} }
pub fn set_background_color(&mut self, color: skia::Color) {
self.background_color = color;
let _ = self.render_all_from_cache();
}
pub fn resize(&mut self, width: i32, height: i32) { pub fn resize(&mut self, width: i32, height: i32) {
let dpr_width = (width as f32 * self.options.dpr()).floor() as i32; let dpr_width = (width as f32 * self.options.dpr()).floor() as i32;
let dpr_height = (height as f32 * self.options.dpr()).floor() as i32; let dpr_height = (height as f32 * self.options.dpr()).floor() as i32;
@ -136,7 +143,7 @@ impl RenderState {
.reset_matrix(); .reset_matrix();
self.final_surface self.final_surface
.canvas() .canvas()
.clear(skia::Color::TRANSPARENT) .clear(self.background_color)
.reset_matrix(); .reset_matrix();
self.debug_surface self.debug_surface
.canvas() .canvas()

View file

@ -1,4 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use skia_safe as skia;
use uuid::Uuid; use uuid::Uuid;
use crate::render::RenderState; use crate::render::RenderState;
@ -57,4 +59,9 @@ impl<'a> State<'a> {
pub fn current_shape(&'a mut self) -> Option<&'a mut Shape> { pub fn current_shape(&'a mut self) -> Option<&'a mut Shape> {
self.current_shape.as_deref_mut() self.current_shape.as_deref_mut()
} }
pub fn set_background_color(&mut self, color: skia::Color) {
self.render_state.set_background_color(color);
self.render_all(true);
}
} }