Giter Site home page Giter Site logo

miladamilli / artcode_poisson_colors Goto Github PK

View Code? Open in Web Editor NEW
31.0 3.0 2.0 790 KB

Generating graphics using Elixir, Phoenix LiveView and SVG

Home Page: https://art-code.herokuapp.com/poisson-colors

Elixir 59.32% CSS 20.12% JavaScript 4.30% HTML 13.70% SCSS 2.56%
elixir liveview phoenix-framework phoenix-liveview svg generative-art

artcode_poisson_colors's Introduction

Generating graphics using Elixir, Phoenix LiveView and SVG

To start your Phoenix server:

  • Install dependencies with mix deps.get
  • Install Node.js dependencies with npm install inside the assets directory
  • Start Phoenix endpoint with mix phx.server

Now you can visit localhost:4000 from your browser.

The App

The app renders circles according to settings (minimum distance of circles, color, opacity, size). You can even save the final image as JPG (and maybe use it as a wallpaper ;-)).

Poisson circles

About building this app

It's a Phoenix LiveView project

New project is created using Mix:

mix phx.new poisson_colors --no-ecto --live

Poisson disc sampling

Circles' positions are calculated using Poisson disc sampling algorithm (even random distribution).

UI & updating settings

Settings are updated using LiveView's phx-change form event, for example:

<form phx-change="color">

and handle_event callback:

def handle_event("color", color, socket) do

Socket is then updated and graphics regenerated according to the new settings:

{:noreply, assign(socket, settings: settings, objects: re_generate_objects(socket.assigns.objects, settings))}

SVG

Each circle has its style, for example:

%{radius: 53, hue: 329, saturation: 72, lightness: 60, opacity: 0.8}

Circles are rendered as SVG elements into .html.leex template

<%= for object <- @objects do %>
<circle cx="<%= object.x %>" cy="<%= object.y %>"
    r="<%= object.style.radius %>"
    fill="hsla(<%= object.style.hue %>, <%= object.style.saturation %>%,
    <%= object.style.lightness %>%, <%= object.style.opacity %>" />
<% end %>

Exporting images to JPG

At first, graphics is exported to a SVG file using EEx.eval_file and .eex template.

EEx.eval_file(@export_template,
  objects: socket.assigns.objects,
  canvas_w: socket.assigns.canvas_w,
  canvas_h: socket.assigns.canvas_h,
  background: socket.assigns.background
)

SVG file is then converted to a JPG using ImageMagick's convert function. (For this to work you need to have ImageMagick installed.)

System.cmd("convert", [file <> ".svg", file <> ".jpg"])

Poisson circles

artcode_poisson_colors's People

Contributors

miladamilli 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

Watchers

 avatar  avatar  avatar

Forkers

adolfont plangora

artcode_poisson_colors's Issues

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.