Code Monkey home page Code Monkey logo

clojure2d's Introduction

clojure2d Clojars Project

Clojure2D is a library supporting generative coding or glitching. It's based on Java2D directly. It's Clojure only, no ClojureScript version.

ex10 ex21 ex5

ex11 ex16 ex19

ex22 ex23

Documentation

Color/Palettes/Gradients

Library contains plenty named colors, palettes and gradients.

There are various sources: colourlovers, thi.ng, paletteer, inigo quilez, cpt-city

Lists:

Examples

200+ examples are in separate repository

https://github.com/Clojure2D/clojure2d-examples

Motivation

This project is the answer to personal needs to optimize my own workflow for generative or glitch creations. I've been producing a lot of Processing code and started to suffer from limitations of working in 'write sketch and run' mode. Too much copy&paste between sketches, zillions of folders, zillions of processed images. And one day I fell in love with FP. This code is the answer.

Is it replacement for...

... quil, thi.ng, clisk, possibly other?

No, rather no. The closest is quil, which is a really great library (both Clojure and Clojurescript, great documentation and webpage, functional mode, 3d support etc.). Clojure2d emphasises math, color and pixel operations and adds a lot of ready to use effects/filters (see extra namespaces`).

What's special in this library then?

In points:

  • Almost decoupled display and canvas (decoupled drawing and refreshing) - you can have as many windows as you want, you can have as many canvases as you want. Display repaints selected canvas automaticaly in separate thread. This way you can operate on the canvas in your own pace.
  • It is still possible to code the Processing way (e.g. you can attach a draw() function to your Display), but draw function keeps state between invocations. This allows you to avoid global state (atoms etc.) and write more functional code. The same is true for events, each Window has an assigned state which is passed through event calls.
  • Easy live coding possible (Emacs/Cider/REPL)
  • FastMath as main math library
  • Main focus on higher level generative/glitch concepts (like sonification support, vector field functions, colorspace operations, things like slitscan etc. See my Processing sketches)

What's odd?

It's kind of a personal library which supports my (probably not optimal, not convenient for others) way of creating stuff. There are still plenty of bugs and not idiomatic code. It may be slower than Processing. Eats a lot of memory (Pixels code is generally immutable).

Installation

Add the following line as a dependency to your project.clj

Usage

Since there are still no tutorials are available, see documentation. All namespaces are described below:

clojure2d.core docs

This is the main namespace with functions in four groups:

  • image file oparations (load/save); jpg, png and bmp
  • canvas operations (wrapper for Java2D BufferedImage and Graphics2D)
  • window and event operations (JFrame wrapper)
  • session handling (logger + unique, sequential filename generator)

clojure2d.pixels docs

Pixels type as a representation of channel values of the image plus operations on pixels. Defines also:

  • pixel filters (blur, erode, dilate, median, threshold, posterize, tint)
  • composing Pixels
  • higher order functions which operate on Pixels parallelly (filter-channels, blend-channels)
  • log density renderer

clojure2d.color docs

  • color space converters
  • huge collection of palettes and gradients

clojure2d.color.blend docs

  • collection of blending basic functions (like add, subtract, divide, difference, etc.)

clojure2d.extra

This is the namespace for common generative/glitch specific libraries:

  • signal - signal processing, wave generators, effects and filters (for sonification process) docs
  • overlays - postprocessing filters (like rgb/crt scanlines, etc.) docs
  • segmentation - image segmentation docs
  • glitch - glitching filters docs
  • utils - visualize objects: palette, gradient, image, vector/scalar field... docs
  • raymarching - simple ray marching 3d scene renderer (abandoned)

generateme/fastmath

All math functions are in fastmath repository.

Community

Clojurians

How to help

Yes! You can help with this project:

  • Beginners:
    • create pixel filters
    • create color spaces
  • Advanced:
    • speed optimizations
    • idiomatic clojure fixes

Discuss about it with me on Slack. Or just Pull Request.

Projects / links

License

Copyright ยฉ 2016-2023 GenerateMe Distributed under the MIT Licence

mixbox is provided under the CC BY-NC 4.0 license for non-commercial use only.

clojure2d's People

Contributors

adicirstei avatar caio-a-garcia avatar emlyn avatar genmeblog avatar tsulej avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clojure2d's Issues

Artifacts in saved images

This is the drawing I see in the window:
2019-11-18-203108_300x400_scrot

This is what I get when saving the image:
2019-11-18--20-27-35

I noticed this before with images that have a lot of small lines. What is happening here? This is the code I use for saving:

(defmethod c2d/key-pressed ["Doodle Seven" \s] [_ _]
  (let [timestamp (.format (java.text.SimpleDateFormat. "yyyy-MM-dd--HH-mm-ss")
                           (java.util.Date.))
        name (str timestamp ".png")]
    (c2d/save (c2d/get-canvas sketch) name)))

PS: I removed the c2d/get-canvas call and am now calling save directly on sketch, which is my window. I tried that yesterday and also had a lot of tearing (complex canvas content with many lines), this now looks better but still a bit broken (notice the upper corner):
2019-11-18--20-33-53

Vars differing only in case cause problems on case-insensitive filesystems

Having two vars that differ only in case causes issues when building a jar on case insensitive file systems, since one class file will overwrite the other. There are two pairs of names causing issues, both in the clojure2d.color namespace:
to-lch / to-LCH
from-lch / from-LCH
I'd be happy to open a PR if you can suggest a suitable new name for one out of each pair of vars.

show-window without window-name doesn't draw

Thanks for this brilliant project.

I was surprised, when I ignored the docstring and called (show-window canvas) without a window-name, that it did display the window (including taking account of the canvas dimensions) but didn't draw in it.

I think it might be something to do with the 1-arity of show-window somehow destructuring a Canvas, but I can't quite nail it down for a PR.

Everything worked when I passed a window-name ๐Ÿ˜€

Example 08 `get-spots` is not defined

Was looking through the examples and number 8 appears to be broken. Apparently, get-spots seems to no longer exist. I greped through the project and can find no reference of it?

ColorProto does not work on Integers

The documentation states that a color can be represented by

Integer - packed ARGB value. Example: 0xffaa01.

But the protocol only implements this for Long values:

This means that eg. mapping clojure2d.color functions over int[] arrays throws an error:

(map clojure2d.color/hue (int-array [0xffaa01]))
;; => No implementation of method: :to-color of protocol: #'clojure2d.color/ColorProto found for class: java.lang.Integer

Is this an oversight or are we expected to manually cast values to long?

Missing dependency prevents clojure2d.core from loading

Hi, I'm facing classpath issues when trying to load the library. I can reproduce the issue from the command line:

$ clj -Sdeps '{:deps {clojure2d {mvn/version "1.4.0"}}}' -e "(require '[clojure2d.core :as clj2d])"
> Syntax error (ClassNotFoundException) compiling new at (fastmath/random.clj:1157:1).
optimization.Uncmin_methods

I'm attempting to fix the issue by manually adding these optimization classes to the classpath, but I am having a hard time tracking them down (the US Fed website that appears to host them appears to be down). Any suggestions here?

revisit clj-kondo hooks for oriented canvas

In the following example clj-kondo reports unued orient binding

(defn apply-body
  [canv orient f]
  (c2d/with-oriented-canvas orient [c (:canvas canv)]
    (c2d/translate c (:shift canv))
    (f c)))

get-state returns nil

Hi! I have the following sketch:

(ns heyarne.line-us.doodles.one
  (:require [clojure2d.core :as c2d]
            [thi.ng.geom.line :as l]))

(defn setup [canvas]
  {:lines []})

(defn draw-state [canvas state]
  (c2d/set-color canvas :white)
  (c2d/rect canvas 0 0 (:w canvas) (:h canvas)))

(defn update-state [canvas state]
  state)

(defn -main [& args]
  (let [canvas (c2d/canvas 400 400)]
    (c2d/show-window
     {:window-name "Doodle One"
      :canvas canvas
      :draw-state (setup canvas)
      :draw-fn (fn [canvas _window _frame state]
                 (draw-state canvas state)
                 (update-state canvas state))})))

(def sketch (-main))

When I'm calling get-state on sketch it returns nil instead of {:lines []}. Am I misunderstanding something?

PS: Thanks for the library, it's super enjoyable!

clojure2d needs a fastmath dep update to work with clojure 1.11

Original problem in the wild: https://ask.clojure.org/index.php/11672/fastmath-errors-in-1-11

Simpler repro with clojure2d:

clj -Sdeps '{:deps {org.clojure/clojure {:mvn/version "1.11.0"} clojure2d/clojure2d {:mvn/version "1.4.3"}}}'

then

(require '[clojure2d.core :as c2d])

yields

WARNING: abs already refers to: #'clojure.core/abs in namespace: fastmath.core, being replaced by: #'fastmath.core/abs
Warning: protocol #'fastmath.protocols/VectorProto is overwriting function abs
WARNING: abs already refers to: #'clojure.core/abs in namespace: fastmath.protocols, being replaced by: #'fastmath.protocols/abs
Reflection warning, fastmath/kernel.clj:72:1 - call to static method divide on fastmath.java.PrimitiveMath can't be resolved (argument types: unknown, double).

.... large number of similar reflection warnings elided
... slf4j warnings elided

Execution error (IllegalStateException) at fastmath.stats$median_absolute_deviation$fn__2570/invokePrim (stats.clj:227).
Attempting to call unbound fn: #'fastmath.core/abs

This problem does not occur with Clojure 1.10.3. Update the fastmath dependency to the latest (2.1.6) is sufficient to fix the problem when used with Clojure 1.11.0.

Add screen-size functions

Maybe something like

 (defn screen-size [] (.getScreenSize (Toolkit/getDefaultToolkit)))
 (defn screen-width [] (.getWidth (screen-size))
 (defn screen-height [] (.getHeight (screen-size))

parallel drawing

Parallel drawing fails. Main reason is usaga Line2D objects as singletons.
Have to create custom type Canvas and hide them there.

Allow for setting alignment of text

A hacky prototype:

(defn text-right
  "Draw text right aligned"
  [canvas ^String s ^long x ^long y]
  (let [width (.stringWidth (.getFontMetrics (.graphics canvas)) s)]
    (clojure2d.core/text canvas s (- x width) y)))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.