How to make amazing generative art with simple JavaScript code

    Rate this content
    Bookmark
    GithubProject website

    Instead of manually drawing each image like traditional art, generative artists write programs that are capable of producing a variety of results. In this workshop you will learn how to create incredible generative art using only a web browser and text editor. Starting with basic concepts and building towards advanced theory, we will cover everything you need to know.

    This workshop has been presented at JS GameDev Summit 2022, check out the latest edition of this JavaScript Conference.

    FAQ

    Generative art refers to art that in whole or in part has been created with the use of an autonomous system. An autonomous system in this context is generally one that can independently determine features of an artwork that would otherwise require decisions made directly by the artist.

    Generative art can be created by using algorithms and computers to generate artwork. This often involves programming and can include techniques like manipulating code to create visual effects, using mathematical formulas to generate patterns, or employing artificial intelligence to create complex designs.

    JavaScript and C++ are commonly used for creating generative art due to their flexibility and the extensive libraries available for creating graphics and animations.

    Yes, generative art can include interactive elements. Interaction can be facilitated through programming, allowing the artwork to change or evolve based on user inputs or environmental data.

    Using different hues and textures adds depth and complexity to generative art, enhancing visual appeal and creating more dynamic and engaging pieces. It allows artists to convey emotions, themes, and abstract concepts through visual variations.

    Common tools for creating generative art include p5.js, Processing, and various web-based editors like CodePen. These tools provide frameworks and libraries that simplify the process of coding and visualizing generatively created artworks.

    Frank Force
    Frank Force
    165 min
    12 Apr, 2022

    Comments

    Sign in or register to post your comment.
    Video Summary and Transcription
    The Workshop on Generative Art and JavaScript covers various topics, including drawing shapes, using the debugger and CSS styling, and exploring variables and colors. It also delves into creating fire effects, drawing lines and shapes, and working with oscillation and particles. The workshop emphasizes the importance of saving work, experimenting, and building up from the basics. It highlights the uses of generative art in different industries and draws inspiration from textiles and the real world. Overall, it provides a comprehensive overview of generative art techniques and their applications.

    1. Introduction to Generative Art and JavaScript

    Short description:

    Hi everybody, I'm Frank Forrest, a generative artist and also a game developer. I've been focusing on learning JavaScript for generative art. I'll show you a website called Twitter, where I post super tiny JavaScript programs. These programs are all generative art and you can experiment, remix ideas, and even edit the code. We'll be working towards creating similar generative art in this workshop.

    Hi everybody, I'm Frank Forrest, a generative artist and also a game developer and I guess a bunch of other stuff. But right now I'm mostly focused on generative art. And I've really been focusing on, especially this year, but even a couple years ago I started getting into learning JavaScript because I had been learning C++ for so long. So I'm not sure if you guys are familiar with the kind of stuff that I do. So I'm just going to give you a quick... show you quickly like what what I've been up to. What I've been up to.

    So, let me see if I can do that. You have to bear with me a little bit because I'm still... I haven't used Zoom that much. So I'm still kind of getting into the flow of things here. Okay, so I hope you can all see my screen now. So I'm showing you a website called Twitter, which is a website to post super tiny JavaScript programs. All these programs are only 140 characters and they're all generative art programs. And I think it's a really great website to experiment and play around with different ideas and see what other people come up with and remix ideas. Like a lot of these are kind of remixes of other people's stuff. But if I... This is not the actual website by the way. This is a website that I made to make it a little bit faster to browse through these things.

    So I've done over a thousand of these. I have covered pretty much every possible top egg you can imagine. I'll sort by my most popular ones. Anything from like a landscape type of thing. This is all generative art and this is the kind of stuff you'll be learning in this workshop. These are all very, very tiny programs. I want to emphasize this here. You can see the code is literally right there on the screen. So if I was to, for example, go to one of these, now you can actually see the code right here and you can even edit the code. This is the kind of thing I'm going to be showing you. We're going to work towards this. So don't I don't expect you to understand exactly what's going on right here. So for example, I could poke these numbers to change kind of what the colors are. Make it more green, or maybe make it more blue or whatever. So this is kind of how I would be maybe tweaking what's going on with this generative art. I can make these instead of bigger square, I can make them like tiny squares, or I could put like a whole lot more of them, maybe. Or whatever. That's what we're going to play around with today.

    2. Creating an HTML File and Drawing Shapes

    Short description:

    We're going to start by creating a new HTML file. I'll show you how to draw a square using the fill rect function. Then I'll show you how to apply color to the shape using HTML color names or hex codes. Now, Chris, you can draw more rectangles if you want.

    So I see a few people have the camera on. So I just want to say hi to Luz and Gleb. Thank you. You know, I don't want to feel like anyone to be obligated to put their camera on. Bartosz, thanks. You know, just feel chill. Do whatever is most comfortable for you. Though it will help me if you feel like you want to share your screen if you're working on something. Because where you're going to be, maybe remixing some of each other's work, I think that would be kind of fun.

    So let's start totally from scratch. We're going to start by just creating a new text file, a new HTML file. So you can do this. You don't need any type of special program. You're just going to right-click. You're going to create a new file. I'm going to call it index.html. You can call it whatever you want. Index.html is kind of like the standard name that's used for HTML files because it will automatically get opened if you go to that folder. So now we have an HTML file. It's an empty file. So I'm going to open up notepad. Now I have notepad++, but I'm just going to use regular notepad here. I'm going to drag it right in there. Now, nothing's in there. So we want to create an HTML file from scratch. And now the thing is, there's a lot of complicated stuff with HTML. And I'm not going to be teaching any of that. I'm just going to go straight into the minimal amount of HTML that you need in order to create generative art. So let's get into it. We got our HTML file here on one side. On the other side, I'm going to open up a web browser. And what I can do is drag the HTML file into the web browser. So now I have a one-to-one. I've HTML on the left, and I have the web browser on the right. So if I was to just type some letters into here, save it, and go over to my web browser and reload it, you'll see that those letters show up in my web browser. Because an HTML file, really is very flexible. And it will automatically open as a text file or whatever you need.

    So let's create a canvas because you don't need a canvas for generative art. You can create generative art with anything you can imagine. But most generative art does use a canvas style drawing system. So in order to create a canvas, it's really that simple, we've just created a canvas. Now we need JavaScript code in order to operate on that canvas. So in order to do that, we create a script block, where do script and use angle bracket tags, and we do a slash script to end the block. Now we've got our our block set up here. And we want to be able to draw to that canvas. So we need to get a 2D context, which is, I'm just going to store it into a variable. I'll talk a little bit more about variables, but we'll store it to a variable, we're going to do X equals C, which is the canvas, when I do IDC, that automatic creates a variable for the canvas. So we're going to do X equals C dot, get context. That's going to give us our 2D context for the canvas. And JavaScript is so flexible, you should end lines on semicolon, because you might rarely have something happen that's not supposed to if you don't do that, but you don't really need semicolons.

    Now I have access to our context. So I'm going to show you how to draw a square, a rect. There's a function called fill rect. And these functions may seem very simple to you. Like how am I supposed to make amazing art with just a fill rect. But really, these are the building blocks. Like at the fundamental level, you will be calling fill rect. And a lot, most of my art uses fill rect or like these simple commands, because at the lowest level, that's really what it comes down to. So fill rect has four parameters, that's just the X and the Y position and the width and the height. So if I say, 100, 100 and then 500 and then 100, so we're going to get a rectangle that's hopefully pretty wide, a little off the side. Now I'm going to hit F5. That's a rectangle. So hopefully everybody has been able to follow so far of how to create a text file, an HTML file that has drawing a shape in it. So this is the very most basic thing. Now, let me show you how to apply color to that shape. Again, we're operating on this object called x. So we've stored our context2d, which is kind of our interface for drawing and stuff to this specific canvas. And it's stored in the variable called x. So I'll do x.DillStyle. Equals read, the word red. And then go over here at 5. They should have a red rectangle, right? So usually, though, you're not going to use names of colors. So this is just supported by HTML. There's a lot of names of colors. Another way to do it is with, if you've ever seen these hex codes. So you would use a hash symbol. And then there, you can either do 6 or 4 or 3 numbers. I usually just use 3. So that's going to be the red, green, and blue components. So if you do F, that would be four red. 0, 0. Let's do them. Let's do a green. So it would be 0 red, full green, and 0 blue. So we've got that. Now, Chris, you can draw more rectangles if you want. So we got one green. I'm just going to copy and paste. And this is not case-sensitive, at least for this part of it, for the color part of it. So I'll do a red rectangle.

    Watch more workshops on topic

    Make a Game With PlayCanvas in 2 Hours
    JSNation 2023JSNation 2023
    116 min
    Make a Game With PlayCanvas in 2 Hours
    Top Content
    Featured WorkshopFree
    Steven Yau
    Steven Yau
    In this workshop, we’ll build a game using the PlayCanvas WebGL engine from start to finish. From development to publishing, we’ll cover the most crucial features such as scripting, UI creation and much more.
    Table of the content:- Introduction- Intro to PlayCanvas- What we will be building- Adding a character model and animation- Making the character move with scripts- 'Fake' running- Adding obstacles- Detecting collisions- Adding a score counter- Game over and restarting- Wrap up!- Questions
    Workshop levelFamiliarity with game engines and game development aspects is recommended, but not required.
    Introduction to WebXR with Babylon.js
    JS GameDev Summit 2022JS GameDev Summit 2022
    86 min
    Introduction to WebXR with Babylon.js
    Workshop
    Gustavo Cordido
    Gustavo Cordido
    In this workshop, we'll introduce you to the core concepts of building Mixed Reality experiences with WebXR and Balon.js.
    You'll learn the following:- How to add 3D mesh objects and buttons to a scene- How to use procedural textures- How to add actions to objects- How to take advantage of the default Cross Reality (XR) experience- How to add physics to a scene
    For the first project in this workshop, you'll create an interactive Mixed Reality experience that'll display basketball player stats to fans and coaches. For the second project in this workshop, you'll create a voice activated WebXR app using Balon.js and Azure Speech-to-Text. You'll then deploy the web app using Static Website Hosting provided Azure Blob Storage.

    Check out more articles and videos

    We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

    The Whimsical Potential of JavaScript Frameworks
    React Summit US 2023React Summit US 2023
    28 min
    The Whimsical Potential of JavaScript Frameworks
    Top Content
    Watch video: The Whimsical Potential of JavaScript Frameworks
    The speaker discusses the whimsical and detailed work of Stripe, particularly their interactive and dynamic pages. They explain the use of React for building whimsical details and tracking mouse position. The speaker introduces React Spring for smooth animation and React3 Fiber for creating a 3D egg model. They also mention the use of Framer Motion and React server components for animating CSS properties.
    Boost the Performance of Your WebGL Unity Games!
    JS GameDev Summit 2023JS GameDev Summit 2023
    7 min
    Boost the Performance of Your WebGL Unity Games!
    The Talk discusses ways to boost the performance of WebGL Unity games, including issues with bundle size, memory usage, and runtime performance. It suggests using Brotli for compression and non-exception support for better performance. Choosing the appropriate texture compression format and experimenting with separate builds can also help. The Talk also covers optimizing textures, models, audio, and assets by reducing build size, using compression, disabling unnecessary models, and optimizing audio quality. Unity's optimization tools and profilers are recommended for analyzing performance and memory issues.
    Emotional & Functional UI Animations in React
    React Day Berlin 2022React Day Berlin 2022
    28 min
    Emotional & Functional UI Animations in React
    Today's Talk discussed the importance of UI animations in React, both functional and emotional. Examples were given using CSS and Framer Motion, with a focus on user feedback and accessibility. Design guidelines and animation usage were highlighted, as well as the consideration of negative emotions in animations. The Talk also touched on designing 404 error pages and concluded with gratitude to the audience and organizers.
    TresJS, a declarative way of creating 3D scenes from Vue components
    Vue.js London 2023Vue.js London 2023
    27 min
    TresJS, a declarative way of creating 3D scenes from Vue components
    The speaker discovered 3D with 3GS and was inspired to create a 3D ecosystem for the Vue community using Troy.js. The process of setting up a 3D scene and objects in Tress was explained, along with creating and configuring 3D objects. The talk also covered animating objects with Tress and the 'Cientos' package, as well as the future of Tress.js and its compatibility with older versions of Three. The speaker expressed gratitude to sponsors and contributors and highlighted the potential for product customization with Tress.js.
    Unreal Engine in WebAssembly/WebGPU
    JS GameDev Summit 2022JS GameDev Summit 2022
    33 min
    Unreal Engine in WebAssembly/WebGPU
    Top Content
    Alex Saint-Louis, co-founder of Wunder Interactive, shares the mission of bringing Unreal Engine to the browser, enabling cross-platform 3D applications and games. They are working on a WebGPU back end for Unreal Engine to push the limits of 3D on the web. Wunder Interactive has improved compression, built their own asset file system, and offers powerful tools for game developers. They are utilizing modern web technologies like WebAssembly, WebGL, and WebGPU, and plan to support other engines like Unity and Godot. The team aims to transform the industry by bringing console-quality games to the browser and providing an alternative distribution path. They are excited to bring Unreal Engine 5 to the web with WebGPU support and are working on WebXR support for cross-platform 3D experiences, including VR.
    From Blender to the Web - the Journey of a 3D Model
    React Advanced 2021React Advanced 2021
    27 min
    From Blender to the Web - the Journey of a 3D Model
    Top Content
    This Talk is about Blender, a free and open-source 3D software, and integrating 3D models into websites. The speaker discusses exporting 3D models to the web using the gltf format and compressing them using gltf transform and draco compression. They also cover integrating the 3D model into a website using React and React 3 fiber, troubleshooting installation and error issues, and working with refs and frames. The Talk explores rendering, lighting, mathematics, and experimentation in 3D, as well as interaction and rendering effects. The speaker also mentions their favorite creation in Blender.