| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
sketch.js is a tiny (~5k) boilerplate for creating JavaScript based creative coding experiments.
We'll also be using coffeescript today to make our code easier to write (less lines!)
Lastly, we'll be using CodePen as our preferred in-browser code editor today. Because this project will utilize coffeescript, sketch.js, and jquery, CodePen will make it easier to configure during our relatively short lesson. If you want some extra credit when we're done, try getting everything to run locally!
Today, we'll be drawing on a canvas and then creating some cool hover effects whenever your mouse touches different elements on the page. There will be a lot of room for experimentation so feel free to create some cool effects of your own!
Visit codepen.io and click the "New Pen" button.
In the top right corner of each section, there is a gear button that will allow you to access the settings for that piece of code. We're going to make a few modifications to make sure our environment is ready for the code we are about to write.
The first thing we want to do is create a place for our drawing.
In the CSS block we have to style our canvas and we'll give it a pretty background color.
CSS
canvas {
background: #023;
display: block;
}JS
# General Variables
sketch = Sketch.create()At this point, you should see a colored background on the bottom half of your codepen screen.
Next, we're going to create a bunch of particles on the screen and have them move about randomly.
JS
This define hat we initially want
# General Variables
sketch = Sketch.create()
particles = []
particleCount = 750
sketch.strokeStyle = 'hsla(200, 50%, 50%, .4)'This describes what a particle is:
# Particles
Particle = ->
this.x = random( sketch.width )
this.y = random( sketch.height, sketch.height * 2 )
this.vx = 0
this.vy = -random( 1, 10 ) / 5
this.radius = 1
this.baseRadius = 1
this.maxRadius = 50
this.threshold = 150 These are the behaviour functions for the particle:
update
Describes what that bubble is suppose to do
render
# Particle Prototype
Particle.prototype =
update: ->
# Adjust Velocity
this.vx += ( random( 100 ) - 50 ) / 1000
this.vy -= random( 1, 20 ) / 10000
# Apply Velocity
this.x += this.vx
this.y += this.vy
render: ->
sketch.beginPath()
sketch.arc( this.x, this.y, this.radius, 0, TWO_PI )
sketch.closePath()
sketch.fill()
sketch.stroke()Now we start populating the bubbles
# Create Particles
z = particleCount
while z--
particles.push( new Particle() )
# Sketch Clear
sketch.clear = ->
sketch.clearRect( 0, 0, sketch.width, sketch.height )
# Sketch Update
sketch.update = ->
i = particles.length
particles[ i ].update() while i--
# Sketch Draw
sketch.draw = ->
i = particles.length
particles[ i ].render() while i--A lot just happened here! Try typing in each line by hand and thinking through what it may be doing. We are creating particles, defining their properties, drawing them on the screen, adjusting their speed, and more.
However, you may notice that the particles disappear after a while. After all, we only created 750. Let's find a way to recycle them.
# General Variables
sketch = Sketch.create()
particles = []
particleCount = 750
sketch.strokeStyle = 'hsla(200, 50%, 50%, .4)'
# Particles
Particle = ->
this.x = random( sketch.width )
this.y = random( sketch.height, sketch.height * 2 )
this.vx = 0
this.vy = -random( 1, 10 ) / 5
this.radius = 1
this.baseRadius = 1
this.maxRadius = 50
this.threshold = 150
# Particle Prototype
Particle.prototype =
update: ->
# Adjust Velocity
this.vx += ( random( 100 ) - 50 ) / 1000
this.vy -= random( 1, 20 ) / 10000
# Apply Velocity
this.x += this.vx
this.y += this.vy
render: ->
sketch.beginPath()
sketch.arc( this.x, this.y, this.radius, 0, TWO_PI )
sketch.closePath()
sketch.fill()
sketch.stroke()
# Create Particles
z = particleCount
while z--
particles.push( new Particle() )
# Sketch Clear
sketch.clear = ->
sketch.clearRect( 0, 0, sketch.width, sketch.height )
# Sketch Update
sketch.update = ->
i = particles.length
particles[ i ].update() while i--
# Sketch Draw
sketch.draw = ->
i = particles.length
particles[ i ].render() while i--You may notice that the particles disappear from the screen after a few seconds, so now we will detect if they are outside our canvas area and put them back on the screen.
JS
# Particle Prototype
Particle.prototype =
update: ->
# Adjust Velocity
this.vx += ( random( 100 ) - 50 ) / 1000
this.vy -= random( 1, 20 ) / 10000
# Apply Velocity
this.x += this.vx
this.y += this.vy
# **New code**
# Check Bounds
if this.x < - this.maxRadius || this.x > sketch.width + this.maxRadius || this.y < - this.maxRadius
this.x = random( sketch.width )
this.y = random( sketch.height + this.maxRadius, sketch.height * 2 )
this.vx = 0
this.vy = -random( 1, 10 ) / 5
render: ->
sketch.beginPath()
sketch.arc( this.x, this.y, this.radius, 0, TWO_PI )
sketch.closePath()
sketch.fill()
sketch.stroke()particleCount = 1234this.radius = 10
this.baseRadius = 10Particle = ->
this.x = random( sketch.width )
this.y = random( sketch.height, sketch.height * 2 )
this.vx = 0
sketch.fillStyle = 'hsla('+random(200)+', 50%, 50%, .4)' # new line
this.vy = -random( 1, 10 ) / 5
this.radius = 10
this.baseRadius = 10
this.maxRadius = 50
this.threshold = 150Here are some extra challenges you can try.
In case you need some help....
| Back | FazBrowse Home | New Git URL |