Skip to content

Commit

Permalink
Applying @jfontan's changes.
Browse files Browse the repository at this point in the history
Sorry @zz85! I'll try to merge your changes at some point too!
  • Loading branch information
mrdoob committed Nov 16, 2011
1 parent d79738e commit 2e2abb0
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 109 deletions.
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'http://rubygems.org'

gem 'sinatra'
gem 'mongo'
gem 'bson_ext'
gem 'json'

25 changes: 25 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
GEM
remote: http://rubygems.org/
specs:
bson (1.4.0)
bson_ext (1.4.0)
json (1.6.1)
mongo (1.4.0)
bson (= 1.4.0)
rack (1.3.5)
rack-protection (1.1.4)
rack
sinatra (1.3.1)
rack (~> 1.3, >= 1.3.4)
rack-protection (~> 1.1, >= 1.1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)

PLATFORMS
ruby

DEPENDENCIES
bson_ext
json
mongo
sinatra
6 changes: 6 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

$: << './server'

require 'main'
run Sinatra::Application

109 changes: 109 additions & 0 deletions server/assets/gallery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>GLSL Sandbox Gallery</title>
<meta charset="utf-8">
<style>
body {
background-color: #000000;
font: 13px Tahoma, Arial, Helvetica, sans-serif;
margin: 3em 4em;
color: #888;
}
a:not(h1){
color: #aaa;
text-decoration: none;
border-bottom: 1px solid #444;
}
a:hover{
color: #009DE9;
border-bottom: 1px solid #009DE9;
}

h1, h2{
color: #009DE9;
font: 28px Tahoma, Arial, Helvetica, sans-serif;;
font-weight: normal;
margin-bottom: 7px;
}
h2{
font-size: 22px;
margin-top: 2em;
}
#gallery{
margin-top: 4em;
}
#gallery>a{
border: none;
}
#gallery img{
margin-right: 3em;
margin-bottom: 2em;
width: 200px;
height: 100px;
border: 1px solid #212121;
}
#gallery img:hover{
box-shadow: 0px 0px 24px 10px #263D65;
border: 1px solid #316FAE;
}
label{
display: block;
margin-top: 1.4em;
margin-bottom: 0.6em;
font-size: 14px;
color: #009DE9;
}
input, textarea{
background: #222;
font-size: 14px;
color: #ccc;
border: none;
padding: 5px 10px;
outline: none;
cursor: pointer;
}
input[type=submit]:hover{
color: #009DE9;
}
</style>
<script>function $(id,m){document.getElementById(id).style.display= m?'block':'none';}</script>
</head>
<body>

<div id="header">
<a href="http://glsl.heroku.com/"><h1>GLSL Sandbox Gallery</h1></a>
<a href="/new">Create new effect!</a> &nbsp;&nbsp;/&nbsp;
<a href="https://github.com/mrdoob/glsl-sandbox">github</a> &nbsp;&nbsp;/&nbsp;
gallery by <a href="http://twitter.com/thevaw">@thevaw</a> and <a href="http://twitter.com/feiss">@feiss</a> &nbsp;/&nbsp; editor by <a href="http://twitter.com/mrdoob">@mrdoob</a>, <a href="http://twitter.com/mrkishi">@mrkishi</a>, <a href="http://twitter.com/p01">@p01</a>, <a href="http://twitter.com/alteredq">@alteredq</a> and <a href="http://twitter.com/kusmabite">@kusmabite</a>
</div>

<div id="gallery">

<% effects.each do |effect| %>
<a href='/<%= effect['_id'] %>/<%= effect['versions'].length-1 %>'><img src='<%= effect['image'] %>'></a>
<% end %>

</div>

<form action="index.php" method="post" accept-charset="utf-8" id="form" enctype="multipart/form-data" style="display: none">
<h2>Send your code</h2>
Use this form or send me the url by twitter to <a href="http://twitter.com/feiss">@feiss</a><br><br>
Do you want to be credited? <br>Don't forget to include comments in your code with your name/nick and contact/link/twitter!<br>
<!-- <label for="thumbnail">Thumbnail</label>
<input type="file" name="thumbnail" value="" id="thumbnail"><br>
(Optional. will be resized to 200x100)<br>
--> <label for="code">Your glsl_sandbox URL</label>
<textarea name="code" rows="5" cols="60" id="code"></textarea><br>

<!--
<label for="thumbnail">Are you a robot?</label>
1 + 9=&nbsp;<input type="text" name="c" value="" id="captcha" size="2"><br><br>
-->
<p><input type="submit" value=" Send ">&nbsp;&nbsp;&nbsp;&nbsp;
<a href="javascript:;" onclick="$('form', false);$('gallery',true)">Cancel</a>
</p>
</form>
<a name="send"></a>
</body>
</html>
75 changes: 75 additions & 0 deletions server/assets/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

var saveButton;

function initialize_compressor(){
return null;
}

function load_url_code() {
if ( window.location.pathname!='/' && window.location.pathname!='/new') {

load_code(window.location.pathname.substr(1));

} else {

code.value = document.getElementById( 'example' ).text;

}
}

function add_save_button() {
saveButton = document.createElement( 'button' );
saveButton.textContent = 'save';
saveButton.addEventListener( 'click', save, false );
toolbar.appendChild( saveButton );
}

function set_save_button(visibility) {
saveButton.style.visibility = visibility;
}

function get_img( width, height ) {
canvas.width = width;
canvas.height = height;
parameters.screenWidth = width;
parameters.screenHeight = height;

gl.viewport( 0, 0, width, height );
createRenderTargets();

render();

img=canvas.toDataURL('image/png');

onWindowResize();

return img;
}

function save() {
img=get_img(200, 100);

data={
"code": document.getElementById( 'code' ).value,
"image": img
}

$.post(window.location.href,
JSON.stringify(data),
function(result) {
window.location.replace('/'+result);
}, "text");
}

function load_code(hash) {
$.get('/item/'+hash, function(result) {
code.value=result;
compile();
});
}

// dummy functions

function setURL(fragment) {
}

4 changes: 4 additions & 0 deletions server/assets/jquery.js

Large diffs are not rendered by default.

161 changes: 161 additions & 0 deletions server/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@

require 'rubygems'
require 'sinatra'
require 'mongo'
require 'json'
require 'erb'

require 'pp'

class Effects
def initialize(effects)
@effects=effects
end

def bind
binding
end

def effects
@effects
end
end

configure do
set :public_folder, 'public'

GALLERY=ERB.new(File.read('server/assets/gallery.html'))

uri = URI.parse(ENV['MONGOHQ_URL'])
conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
db = conn.db(uri.path.gsub(/^\//, ''))
VERSIONS=db.collection('versions')
CODE=db.collection('code')
COUNTERS=db.collection('counters')

# initialize counters
code=COUNTERS.find_one({:_id => 'code'})
if !code
COUNTERS.insert({
:_id => 'code',
:counter => 0
})
end
end

def increment_code_counter
counter=COUNTERS.find_and_modify({
:query => {:_id => 'code'},
:update => {'$inc' => {:counter => 1}}
})

counter['counter']
end

def save_version(code_id, code)
time=Time.now
code_data=JSON.parse(code)

data={
:created_at => time,
:code => code_data['code']
}

CODE.find_and_modify({
:query => { :_id => code_id },
:update => {
'$set' => {
:modified_at => time,
:image => code_data['image']
},
'$push' => { :versions => data }
}
})
end

get '/' do
effects=CODE.find({}, {:sort => [:modified_at, 'descending']})

ef=Effects.new(effects)

GALLERY.result(ef.bind)
end

# assets

get '/new' do
send_file 'static/index.html'
end

get %r{^.*/js/jquery.js$} do
send_file 'server/assets/jquery.js'
end

get %r{^.*/js/helpers.js$} do
send_file 'server/assets/helpers.js'
end

get %r{^.*/js/lzma.js$} do
"\n"
end

get %r{^/(\d+)(/(\d+))?$} do
send_file 'static/index.html'
end

get %r{/item/(\d+)(/(\d+))?} do
code_id=params[:captures][0].to_i
if params[:captures][1]
version_id=params[:captures][2].to_i
else
version_id=nil
end

code=CODE.find_one({:_id => code_id})

if version_id
item=code['versions'][version_id]
else
item=code['versions'].last
end

if item
item['code']
else
'// item not found'
end
end

post %r{^/(new)$} do
counter=increment_code_counter
body=request.body.read

data={
:_id => counter,
:created_at => Time.now,
:modified_at => Time.now,
:versions => []
}

CODE.insert(data)

save_version(counter, body)

"#{counter}/0"
end


post %r{^/(\d+)(/(\d+))?$} do
code_id=params[:captures][0].to_i
body=request.body.read
save_version(code_id, body)

code=CODE.find_one({ :_id => code_id })

version=code['versions'].length-1

"#{code_id}/#{version}"
end



Loading

0 comments on commit 2e2abb0

Please sign in to comment.