coffee-processing 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in coffee-processing.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Junegunn Choi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ coffee-processing
2
+ =================
3
+
4
+ Helps writing Processing.js sketches in Coffeescript.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'coffee-processing'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install coffee-processing
20
+
21
+ Usage
22
+ -----
23
+
24
+ A sample Processing.js sketch written in Coffeescript with the help of coffee-processing.
25
+ ```coffee
26
+ setup = ->
27
+ size $(window).width(), $(window).height()
28
+ frameRate 30
29
+ background 255
30
+
31
+ draw = ->
32
+ for i in [0..10]
33
+ s = random(100)
34
+ stroke random(255), random(255), random(255)
35
+ ellipse random(width()), random(height()), s, s
36
+ ```
37
+
38
+ Ruby code for compiling it into Javascript.
39
+ ```ruby
40
+ require 'coffee-processing'
41
+
42
+ File.open('compiled.js', 'w') do |f|
43
+ f << CoffeeProcessing.compile('this.sketch', code)
44
+ end
45
+ ```
46
+
47
+ And the HTML page.
48
+ ```html
49
+ <script src='compiled.js' type='text/javascript'></script>
50
+ <canvas id='sketch'></canvas>
51
+ <script type='text/javascript'>
52
+ var processing = new Processing(document.getElementById("sketch"), this.sketch)
53
+ </script>
54
+ ```
55
+
56
+ coffee-processing script
57
+ ------------------------
58
+
59
+ ```
60
+ usage: coffee-processing [--template] <js object name> <sketch file>
61
+
62
+ --template Create a template page for the sketch
63
+ -h, --help Show this message
64
+ ```
65
+
66
+
67
+ Caveats
68
+ -------
69
+
70
+ Instance variables of Processing object, such as `width`, `frameCount` and `__mousePressed` (among others)
71
+ should be accessed through their corresponding shortcut functions as follows.
72
+
73
+ ```coffee
74
+ # P3D, __mousePressed, frameCount, width and height are functions, not values.
75
+
76
+ setup = ->
77
+ size 100, 100, P3D()
78
+
79
+ draw = ->
80
+ if __mousePressed()
81
+ point frameCount() % width(), frameCount() % height()
82
+ ```
83
+
84
+ Or equivalently, you can access them as the properties of `processing` object.
85
+ This is slightly more efficient.
86
+
87
+ ```coffee
88
+ # Alias for processing instance
89
+ p5 = processing
90
+
91
+ setup = ->
92
+ size 100, 100, p5.P3D
93
+
94
+ draw = ->
95
+ if p5.__mousePressed
96
+ point p5.frameCount % p5.width, p5.frameCount % p5.height
97
+ ```
98
+
99
+ Examples
100
+ --------
101
+
102
+ Check out [examples](https://github.com/junegunn/coffee-processing/tree/master/examples) directory.
103
+
104
+ Contributing
105
+ ------------
106
+
107
+ 1. Fork it
108
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
109
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
110
+ 4. Push to the branch (`git push origin my-new-feature`)
111
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ desc 'Run tests'
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'lib'
8
+ t.pattern = 'test/**/test_*.rb'
9
+ t.verbose = true
10
+ t.warning = true
11
+ end
12
+
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib') # FIXME
4
+ require 'rubygems'
5
+ require 'coffee-processing'
6
+ require 'optparse'
7
+
8
+ options = {}
9
+ op = OptionParser.new do |opts|
10
+ opts.banner =
11
+ "usage: coffee-processing [--template] <js object name> <sketch file>"
12
+ opts.separator ''
13
+
14
+ opts.on('--template', 'Create a template page for the sketch') do |v|
15
+ options[:template] = v
16
+ end
17
+
18
+ opts.on('-h', '--help', 'Show this message') do
19
+ puts opts
20
+ exit 0
21
+ end
22
+ end
23
+
24
+ op.parse! ARGV
25
+
26
+ if ARGV.length != 2
27
+ puts op
28
+ exit 1
29
+ end
30
+
31
+ unless File.exists?(ARGV[1])
32
+ puts "#{ARGV[1]} does not exist."
33
+ puts op
34
+ exit 1
35
+ end
36
+
37
+ if options[:template]
38
+ name = File.basename(ARGV[1]).chomp(File.extname(ARGV[1]))
39
+ CoffeeProcessing.generate_template_page ARGV[0], File.read(ARGV[1]), name
40
+ puts "Created #{name} directory."
41
+ else
42
+ puts CoffeeProcessing.compile ARGV[0], File.read(ARGV[1])
43
+ end
44
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/coffee-processing/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Junegunn Choi"]
6
+ gem.email = ["junegunn.c@gmail.com"]
7
+ gem.description = %q{Helps writing Processing.js sketches in Coffeescript}
8
+ gem.summary = %q{Helps writing Processing.js sketches in Coffeescript}
9
+ gem.homepage = "https://github.com/junegunn/coffee-processing"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "coffee-processing"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = CoffeeProcessing::VERSION
17
+
18
+ gem.add_runtime_dependency 'coffee-script', '~> 2.2.0'
19
+ gem.add_runtime_dependency 'erubis', '~> 2.7.0'
20
+ gem.add_development_dependency 'test-unit', '~> 2.4.8'
21
+ end
@@ -0,0 +1,12 @@
1
+ # Author: Junegunn Choi (junegunn.c@gmail.com)
2
+
3
+ setup = ->
4
+ size $(window).width(), $(window).height()
5
+ frameRate 30
6
+ background 255
7
+
8
+ draw = ->
9
+ for i in [0..10]
10
+ s = random(100)
11
+ stroke random(255), random(255), random(255)
12
+ ellipse random(width()), random(height()), s, s
data/examples/generate ADDED
@@ -0,0 +1,8 @@
1
+ #!/bin/bash
2
+ #
3
+ # Junegunn Choi (junegunn.c@gmail.com)
4
+ # 2012/04/25-
5
+
6
+ ../bin/coffee-processing --template this.sketch dots.coffee
7
+ ../bin/coffee-processing --template this.sketch lines.coffee
8
+
@@ -0,0 +1,47 @@
1
+ # Author: Junegunn Choi (junegunn.c@gmail.com)
2
+
3
+ steps = 5
4
+ count = 200
5
+ offset = 0
6
+ status = 0
7
+
8
+ exag = (v, times) ->
9
+ v = 6 * pow(v, 5) -
10
+ 15 * pow(v, 4) +
11
+ 10 * pow(v, 3) for i in [0...times]
12
+ v
13
+
14
+ setup = ->
15
+ size $(window).width(), $(window).height()
16
+ frameRate 30
17
+ colorMode RGB(), 1.0
18
+ background 1
19
+ stroke 0, 0.1
20
+
21
+ mousePressed = ->
22
+ if status == 0
23
+ status = 1
24
+ else
25
+ offset = frameCount() * steps % width()
26
+ background 1
27
+ status = 0
28
+
29
+ draw = ->
30
+ w = width()
31
+ h = height()
32
+
33
+ if status != 2
34
+ for s in [0...steps]
35
+ x = (frameCount() - 1) * steps + s
36
+ for i in [0...count]
37
+ point(
38
+ (x - offset) % w,
39
+ h / 2 +
40
+ h * (exag(noise(i / count, x / w), 2) - 0.5)
41
+ )
42
+
43
+ if status == 1 && (x - offset) % w == 0
44
+ status = 2
45
+ break
46
+ null
47
+
@@ -0,0 +1,44 @@
1
+ require "coffee-processing/version"
2
+ require 'erubis'
3
+ require 'coffee-script'
4
+
5
+ module CoffeeProcessing
6
+ def self.compile javascript_object, code
7
+ first_line = code.lines.map(&:chomp).find { |line| !line.empty? }
8
+
9
+ if first_line && indentation = first_line.scan(/^\s+/).first
10
+ code = code.lines.map { |line| line.sub(/^#{indentation}/, '')}.join
11
+ end
12
+
13
+ code = code.lines.map { |line| "#{line} " }.join
14
+ code = code.strip
15
+
16
+ CoffeeScript.compile(
17
+ Erubis::Eruby.new(
18
+ File.read File.join(
19
+ File.dirname(__FILE__),
20
+ 'coffee-processing',
21
+ 'boilerplate.coffee.erb'
22
+ )
23
+ ).result(binding)
24
+ )
25
+ end
26
+
27
+ def self.generate_template_page javascript_object, code, output_dir
28
+ require 'fileutils'
29
+ FileUtils.mkdir_p output_dir
30
+ File.open(File.join(output_dir, 'sketch.js'), 'w') do |f|
31
+ f << compile(javascript_object, code)
32
+ end
33
+ File.open(File.join(output_dir, 'index.html'), 'w') do |f|
34
+ f <<
35
+ Erubis::Eruby.new(
36
+ File.read File.join(
37
+ File.dirname(__FILE__),
38
+ 'coffee-processing',
39
+ 'template.html.erb'
40
+ )
41
+ ).result(binding)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,26 @@
1
+ <%= javascript_object %> = (processing) ->
2
+ for __sym of processing when eval("typeof #{__sym} === 'undefined'")
3
+ if typeof processing[__sym] == 'function'
4
+ eval "var #{__sym} = function() { return processing.#{__sym}.apply(processing, arguments) }"
5
+ else
6
+ eval "var #{__sym} = function() { return processing.#{__sym} }"
7
+ null
8
+
9
+ <%= code %>
10
+
11
+ for callback in [
12
+ 'setup',
13
+ 'draw',
14
+ 'mouseClicked',
15
+ 'mouseDragged',
16
+ 'mouseMoved',
17
+ 'mouseOut',
18
+ 'mouseOver',
19
+ 'mousePressed',
20
+ 'mouseReleased',
21
+ 'keyPressed',
22
+ 'keyReleased',
23
+ 'keyTyped'
24
+ ] when !eval("typeof #{__sym} === 'undefined'")
25
+ eval "processing.#{callback} = #{callback}"
26
+
@@ -0,0 +1,17 @@
1
+ <html>
2
+ <head>
3
+ <title>
4
+ coffee-processing sketch
5
+ </title>
6
+ <script src='https://raw.github.com/processing-js/processing-js/master/processing.js' type='text/javascript'></script>
7
+ <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' type='text/javascript'></script>
8
+ <script src='sketch.js' type='text/javascript'></script>
9
+ </head>
10
+ <body>
11
+ <canvas id='sketch' style='position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: black'>
12
+ </canvas>
13
+ <script type='text/javascript'>
14
+ var processing = new Processing(document.getElementById("sketch"), <%= javascript_object %>)
15
+ </script>
16
+ </body>
17
+ </html>
@@ -0,0 +1,3 @@
1
+ module CoffeeProcessing
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib')
4
+ require 'test-unit'
5
+ require 'coffee-processing'
6
+
7
+ class TestCoffeeProcessing < Test::Unit::TestCase
8
+ def test_indentation
9
+ without_indentation = "
10
+ setup = ->
11
+ null
12
+
13
+ draw = ->
14
+ null"
15
+
16
+ with_space_indentation = "
17
+ setup = ->
18
+ null
19
+
20
+ draw = ->
21
+ null
22
+ "
23
+
24
+ with_tab_indentation = "
25
+ setup = ->
26
+ null
27
+
28
+ draw = ->
29
+ null
30
+ "
31
+
32
+ assert_equal 1,
33
+ [without_indentation, with_space_indentation, with_tab_indentation].map { |code|
34
+ CoffeeProcessing.compile 'this.sketch', code
35
+ }.uniq.length
36
+ end
37
+
38
+ def test_error
39
+ pend('Need to adjust line number (-8) in exception message') do
40
+ CoffeeProcessing.compile 'this.sketch', '-> = ->'
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coffee-processing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Junegunn Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: coffee-script
16
+ requirement: &2151894180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2151894180
25
+ - !ruby/object:Gem::Dependency
26
+ name: erubis
27
+ requirement: &2151893340 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.7.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2151893340
36
+ - !ruby/object:Gem::Dependency
37
+ name: test-unit
38
+ requirement: &2151892320 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.4.8
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2151892320
47
+ description: Helps writing Processing.js sketches in Coffeescript
48
+ email:
49
+ - junegunn.c@gmail.com
50
+ executables:
51
+ - coffee-processing
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - bin/coffee-processing
61
+ - coffee-processing.gemspec
62
+ - examples/dots.coffee
63
+ - examples/generate
64
+ - examples/lines.coffee
65
+ - lib/coffee-processing.rb
66
+ - lib/coffee-processing/boilerplate.coffee.erb
67
+ - lib/coffee-processing/template.html.erb
68
+ - lib/coffee-processing/version.rb
69
+ - test/test_coffee_processing.rb
70
+ homepage: https://github.com/junegunn/coffee-processing
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.11
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Helps writing Processing.js sketches in Coffeescript
94
+ test_files:
95
+ - test/test_coffee_processing.rb