Skip to content

Commit

Permalink
updates for v0.026
Browse files Browse the repository at this point in the history
  • Loading branch information
cormullion committed Nov 12, 2020
1 parent c71b3fe commit 4fd88b1
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 41 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Changelog

## [v2.6.0] - forthcoming
## [v2.6.0] - 12 November 2020

### Added

- additional methods for `offsetpoly()` for open polylines
- experimental image_as_matrix!()
- @play macro
- image_as_matrix!() - reusable buffer

### Changed

Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ makedocs(
"Images" => "images.md",
"Turtle graphics" => "turtle.md",
"Animation" => "animation.md",
"Live graphics" => "livegraphics.md",
"More examples" => "moreexamples.md",
"Index" => "functionindex.md"
]
Expand Down
Binary file added docs/src/assets/figures/clock.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions docs/src/livegraphics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Live graphics

With the help of an external appication to manage windows, it's possible to use Luxor to create continuously changing graphics in a window.

This example uses the [MiniFB](https://github.com/aviks/MiniFB.jl) package, which you can add using `] add MiniFB`.

The file `play.jl` defines a simple macro, `@play`, which continuously evaluates and draws the graphics in a window. For example, this code:

```
using Luxor
include(dirname(pathof(Luxor)) * "/play.jl")
let θ = 0
@play 400 400 begin
#
background("black")
sethue("white")
rotate(θ)
hypotrochoid(200, 110, 37, :stroke)
θ += π/120
sleep(0.01)
#
end
end
```

draws a continuously rotating hypotrochoid.

## Clock

This code also imports the `@play` macro.

The call to `sleep()` reduces the CPU time, and allows other processes to run, but the millisecond
animation will be less smooth as a result.

![clock](assets/figures/clock.gif)

```
using Luxor, Colors, Dates, ColorSchemes
include(dirname(pathof(Luxor)) * "/play.jl")
function clock(cscheme=ColorSchemes.leonardo)
@play 400 600 begin
# background
sethue(get(cscheme, .0))
paint()
# 24hour sector
fontsize(30)
sethue(get(cscheme, .2))
h = Dates.hour(now())
sector(O, 180, 200, π/2, π/2 + rescale(h, 0, 24, 0, 2pi), :fill)
@layer begin
fontsize(12)
sethue("white")
@. text(["0", "6", "12", "18"], polar(190, [i * π/2 for i in 1:4]),
halign=:center,
valign=:middle)
end
# minute sector
sethue(get(cscheme, .4))
m = Dates.minute(now())
sector(O, 160, 180, 3π/2, 3π/2 + rescale(m, 0, 60, 0, 2pi), :fill)
# second sector
sethue(get(cscheme, .6))
s = Dates.second(now())
sector(O, 140, 160, 3π/2, 3π/2 + rescale(s, 0, 60, 0, 2pi), :fill)
# millisecond indicator
@layer begin
setopacity(0.5)
sethue(get(cscheme, .8))
ms = Dates.value(Dates.Millisecond(Dates.now()))
circle(polar(120, 3π/2 + rescale(ms, 0, 1000, 0, 2pi)), 20, :fill)
end
# central text
fontface("JuliaMono-Black")
sethue(get(cscheme, 1.0))
text(Dates.format(Dates.now(), "HH:MM:SS"), halign=:center)
sleep(0.05)
end
end
clock(ColorSchemes.klimt)
```
4 changes: 1 addition & 3 deletions src/Luxor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ module Luxor

using Juno, Cairo, Colors, FileIO, Dates

# , MiniFB

#= from Cairo use: CairoARGBSurface, CairoEPSSurface, CairoMatrix, CairoPDFSurface, CairoPattern, CairoPatternMesh, CairoSurface, CairoSVGSurface,
CairoContext, arc, arc_negative, circle, clip, clip_preserve, close_path,
convert_cairo_path_data, copy_path, copy_path_flat, curve_to, destroy, fill,
Expand Down Expand Up @@ -54,8 +52,8 @@ include("Boxmaptile.jl")
include("noise.jl")
include("deprecations.jl")
include("graphlayout.jl")
# include("play.jl")
include("Style.jl")
# include("play.jl") # will require MiniFB
#include("shapefile.jl") # don't load unless you've loaded Shapefile.jl

export Drawing,
Expand Down
103 changes: 68 additions & 35 deletions src/play.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
using MiniFB

function onclick(window, button, mod, isPressed)::Cvoid
if Bool(isPressed)
println("mouse clicked")
end
mousex = mfb_get_mouse_x(window)
mousey = mfb_get_mouse_y(window)
println("x: $mousex y: $mousey")
end

function active_fn(win::Ptr{Cvoid}, is_active::Bool)
println("Window is now ", is_active ? "" : "in", "active")
end

macro play(w, h, body)
quote
window = mfb_open_ex("julia", $w, $h, MiniFB.WF_RESIZABLE)
window = mfb_open_ex("Luxor -> Julia", $w, $h, MiniFB.WF_RESIZABLE)

mfb_set_active_callback(window, active_fn)
mfb_set_mouse_button_callback(window, onclick)

buffer = zeros(UInt32, $w, $h)
while true
Drawing($w, $h, :image)
Expand All @@ -17,7 +36,51 @@ macro play(w, h, body)
end
end

#= bez path thingy
#= Examples:
1 clock
using Luxor, Colors, Dates, ColorSchemes
include(dirname(pathof(Luxor)) * "/play.jl")
function clock(cscheme=ColorSchemes.leonardo)
@play 400 600 begin
fontface("JuliaMono-Regular")
sethue(get(cscheme, .0))
paint()
fontsize(30)
sethue(get(cscheme, .2))
h = Dates.hour(now())
sector(O, 180, 200, 3π/2, 3π/2 + rescale(h, 0, 24, 0, 2pi), :fill)
sethue(get(cscheme, .4))
m = Dates.minute(now())
sector(O, 160, 180, 3π/2, 3π/2 + rescale(m, 0, 60, 0, 2pi), :fill)
sethue(get(cscheme, .6))
s = Dates.second(now())
sector(O, 140, 160, 3π/2, 3π/2 + rescale(s, 0, 60, 0, 2pi), :fill)
sethue(get(cscheme, .8))
ms = Dates.value(Dates.Millisecond(Dates.now()))
sector(O, 137, 140, 3π/2, 3π/2 + rescale(ms, 0, 1000, 0, 2pi), :fill)
sethue(get(cscheme, 1.0))
text(Dates.format(Dates.now(), "HH:MM:SS"), halign=:center)
end
end
clock(ColorSchemes.botticelli)
=#


#=
2: bezier path thingy
using Luxor, Colors, Dates
Expand Down Expand Up @@ -66,7 +129,9 @@ bez()
=#

#= some balls
#=
3 some balls
using Luxor, Colors, Dates
Expand Down Expand Up @@ -106,35 +171,3 @@ end
f()
=#

#= clock
using Luxor, Colors, Dates
function clock()
@play 400 600 begin
fontface("JuliaMono-Regular")
# outer
sethue("black")
paint()
sethue("white")
fontsize(30)
text(Dates.format(Dates.now(), "HH:MM:SS"), halign=:center)
sethue("cyan")
h = Dates.hour(now())
sector(O, 180, 200, 3π/2, 3π/2 + rescale(h, 0, 24, 0, 2pi), :fill)
sethue("magenta")
m = Dates.minute(now())
sector(O, 160, 180, 3π/2, 3π/2 + rescale(m, 0, 60, 0, 2pi), :fill)
sethue("red")
s = Dates.second(now())
sector(O, 140, 160, 3π/2, 3π/2 + rescale(s, 0, 60, 0, 2pi), :fill)
sethue("orange")
ms = Dates.value(Dates.Millisecond(Dates.now()))
sector(O, 137, 140, 3π/2, 3π/2 + rescale(ms, 0, 1000, 0, 2pi), :fill)
end
end
clock()
=#

0 comments on commit 4fd88b1

Please sign in to comment.