Skip to content

Instantly share code, notes, and snippets.

@atleastimtrying
Created November 11, 2011 10:53
Show Gist options
  • Save atleastimtrying/1357730 to your computer and use it in GitHub Desktop.
Save atleastimtrying/1357730 to your computer and use it in GitHub Desktop.
noddling about with twitter for wilson
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
window.sketch = (p5)->
p5.setup = ->
p5.size $(window).width(),$(window).height()
p5.background 0x000033
p5.smooth()
p5.rectMode p5.CENTER
@x = $('canvas').attr('data-x')
@y = $('canvas').attr('data-y')
@diameter = $('canvas').attr('data-diameter')
@is_square = $('canvas').attr('data-is-square') is 'true'
p5.draw = ->
p5.background 0x000033
if @is_square
p5.rect @x, @y, @diameter, @diameter
else
p5.ellipse @x,@y,@diameter, @diameter
class window.View
constructor: ->
@canvas = $("canvas")
@processing = new Processing @canvas[0], window.sketch
@activate_mouse() if @canvas.attr('data-new') is 'true'
$(window).resize @setCanvasSize
activate_mouse: ->
@processing.mousePressed = ->
@x = window.view.processing.mouseX
@y = window.view.processing.mouseY
@processing.mouseDragged = ->
@x = window.view.processing.mouseX
@y = window.view.processing.mouseY
set_radios: (event)=>
@processing.is_square = event.currentTarget.value is 'true'
showui: ->
$('#ui').html '<p>diameter: <div id="diameter"></div></p>
<input type="radio" name="is_square" value="true"/> square
<input type="radio" name="is_square" value="false"/> circle'
$("#diameter").slider {
slide: @update_diameter
change: @update_diameter
max: 300
range: 'min'
}
$('input:radio[name=is_square]').change @set_radios
update_diameter: =>
@processing.diameter = $('#diameter').slider 'value'
hideui: ->
$('#ui').html ''
addSaveButtons: ->
$('header').append '<p><a href="#" id="save">Save</a> <a href="#" id="signout">Sign out of twitter</a></p>'
$('#save').live "click", window.controller.create
$('#signout').live "click", =>
twttr.anywhere.signOut()
false
removeSaveButtons: ->
$('#save').remove()
$('#signout').remove()
showUser: (user)->
window.currentUser = user
$('#user-image').html "<img src='#{window.currentUser.attributes.profile_image_url}'>"
$('#user-name').html user.screenName
$('header').css {
"color" : "##{window.currentUser.attributes.profile_text_color}"
}
$(".inner_header").css {
"background-color" : "##{window.currentUser.attributes.profile_background_color}"
}
$('header').css({"background-image" : "url(#{window.currentUser.attributes.profile_background_image_url})"}) if window.currentUser.attributes.profile_background_tile
window.controller.fetchSketch user.screenName
@addSaveButtons()
@showui()
killUser: ->
window.currentUser = ''
$('#user-image').html ''
$('#user-name').html ''
@removeSaveButtons()
@hideui()
setCanvasSize: =>
@processing.size $(window).width(),$(window).height()
@canvas.css {
height : $(window).height()
width : $(window).width()
}
class window.Controller
constructor: ->
twttr.anywhere @twitter
fetchSketch: (username)->
$.post '/read', {
'username': username
}, (response)->
window.view.processing.x = response.x
window.view.processing.y = response.y
window.view.processing.diameter = response.diameter
$('#diameter').slider 'value' : response.diameter
window.view.processing.is_square = response.is_square
$('input:radio[name=is_square]').prop 'checked',false
$("input:radio[value='#{response.is_square}']").prop 'checked', true
create: ->
$.post '/create', {
'username': window.currentUser.screenName
'x': window.view.processing.x
'y': window.view.processing.y
'is_square': window.view.processing.is_square
'diameter': window.view.processing.diameter
}
twitter: (T)->
T("#twitter-sign-in").connectButton {
authComplete: (user)->
window.view.showUser T.currentUser
signOut: ->
window.view.killUser()
}
if T.isConnected() and $('canvas').attr('data-new') is 'true'
window.view.showUser T.currentUser
$ ->
window.view = new View()
window.controller = new Controller()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment