Skip to content

Commit

Permalink
Add SVG example (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 authored Mar 18, 2022
1 parent d46a1ce commit 48bdd72
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"examples/higher-order-components",
"examples/motion",
"examples/ssr",
"examples/svg",
"examples/timer",
"examples/todomvc",
"examples/transitions",
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Now open up `localhost:8080` in your browser to see "Hello World!".
| [js-framework-benchmark](js-framework-benchmark) | Implementation of [js-framework-benchmark](https://github.com/krausest/js-framework-benchmark) |
| [motion](motion) | Demonstration for using animation frames and tweened signals |
| [ssr](ssr) | Demonstration of server-side-rendering |
| [svg](svg) | Creating SVGs with the `view!` macro |
| [timer](timer) | Demonstration of using futures to auto-increment a counter |
| [todomvc](todomvc) | Fully compliant implementation of [TodoMVC](https://todomvc.com/) spec |
| [transitions](transitions) | Suspense + async transitions |
Expand Down
9 changes: 9 additions & 0 deletions examples/svg/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "svg"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sycamore = { path = "../../packages/sycamore" }
10 changes: 10 additions & 0 deletions examples/svg/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>SVG | Sycamore</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body></body>
</html>
29 changes: 29 additions & 0 deletions examples/svg/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::time::Duration;

use sycamore::motion::ScopeMotionExt;
use sycamore::{easing, prelude::*};

#[component]
fn App<G: Html>(ctx: Scope) -> View<G> {
let rotate = ctx.create_tweened_signal(0f64, Duration::from_millis(800), easing::quad_inout);

view! { ctx,
button(disabled=rotate.is_tweening(), on:click=|_| rotate.set(*rotate.get() + 0.5)) { "Half rotate..." }
button(disabled=rotate.is_tweening(), on:click=|_| rotate.set(*rotate.get() + 1.0)) { "Rotate!" }
button(disabled=rotate.is_tweening(), on:click=|_| rotate.set(*rotate.get() + 2.0)) { "Rotate twice!!" }
button(disabled=rotate.is_tweening(), on:click=|_| rotate.set(*rotate.get() + 3.0)) { "Rotate thrice!!!" }
svg(height="210", width="500", xmlns="http://www.w3.org/2000/svg") {
rect(
x="100", y="100",
width="100", height="100",
fill="red", transform=format!("rotate({}, 150, 150)", (*rotate.get() * 360.0) as u32)
)
}
}
}

fn main() {
sycamore::render(|ctx| {
view! { ctx, App() }
});
}
3 changes: 2 additions & 1 deletion packages/sycamore/src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ define_elements! {
wbr {},
}

// A list of valid SVG elements. Some elements are commented out because they conflict with the HTML elements.
// A list of valid SVG elements. Some elements are commented out because they conflict with the HTML
// elements.
define_elements! {
Some("http://www.w3.org/2000/svg"),
svg {},
Expand Down
12 changes: 12 additions & 0 deletions packages/sycamore/src/motion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ struct TweenedInner<'a, T: Lerp + Clone + 'a> {
/// context to be able to spawn the raf callback.
ctx: Scope<'a>,
value: RcSignal<T>,
is_tweening: RcSignal<bool>,
raf_state: Option<RafState<'a>>,
transition_duration_ms: f32,
easing_fn: Rc<dyn Fn(f32) -> f32>,
Expand All @@ -192,6 +193,7 @@ impl<'a, T: Lerp + Clone + 'a> Tweened<'a, T> {
Self(Rc::new(RefCell::new(TweenedInner {
ctx,
value,
is_tweening: create_rc_signal(false),
raf_state: None,
transition_duration_ms: transition_duration.as_millis() as f32,
easing_fn: Rc::new(easing_fn),
Expand All @@ -212,6 +214,7 @@ impl<'a, T: Lerp + Clone + 'a> Tweened<'a, T> {

let start_time = Date::now();
let signal = self.0.borrow().value.clone();
let is_tweening = self.0.borrow().is_tweening.clone();
let transition_duration_ms = self.0.borrow().transition_duration_ms;

// If previous raf is still running, call stop() to cancel it.
Expand All @@ -232,10 +235,12 @@ impl<'a, T: Lerp + Clone + 'a> Tweened<'a, T> {
true
} else {
signal.set(new_value.clone());
is_tweening.set(false);
false
}
});
start();
self.0.borrow().is_tweening.set(true);
self.0.borrow_mut().raf_state = Some((running, start, stop));
}

Expand All @@ -253,6 +258,12 @@ impl<'a, T: Lerp + Clone + 'a> Tweened<'a, T> {
pub fn signal(&self) -> RcSignal<T> {
self.0.borrow().value.clone()
}

/// Returns `true` if the value is currently being tweened/interpolated. This value is reactive
/// and can be tracked.
pub fn is_tweening(&self) -> bool {
*self.0.borrow().is_tweening.get()
}
}

impl<'a, T: Lerp + Clone + 'static> Clone for Tweened<'a, T> {
Expand All @@ -266,6 +277,7 @@ impl<'a, T: Lerp + Clone + 'static> Clone for TweenedInner<'a, T> {
Self {
ctx: self.ctx,
value: self.value.clone(),
is_tweening: self.is_tweening.clone(),
raf_state: self.raf_state.clone(),
transition_duration_ms: self.transition_duration_ms,
easing_fn: Rc::clone(&self.easing_fn),
Expand Down

0 comments on commit 48bdd72

Please sign in to comment.