Giter Site home page Giter Site logo

choo-animals's Introduction

choo-animals's People

Contributors

louiscenter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

choo-animals's Issues

hard to tell what animal is being selected, maybe highlight current animal

maybe highlight current animal type, so you know what you are clicking...
new main.js, code is messy since I'm learning choo & javascript...see create html template code area

main.js:
// import choo's template helper
var html = require('choo/html')

// import template
var animal = require('./animal.js')

// export module
module.exports = function (state, emit) {
// create html template
state.c=["","","","","",""]
var animals=['all','crocodile','koala','lion','tiger','walrus']
state.c[animals.indexOf(state.params.type || 'all' )] = 'highlight'

return html '

    <div class="grass">
      <img src="/assets/bg.gif" onclick=${add} />
      ${state.animals.map(animalMap)}
    </div>
    <div class="controls">
      <ul class="filters">
        <li><a class=${state.c[0]}  href="/">all</a></li>
        <li><a class=${state.c[1]}  href="/filter/crocodile">crocodiles</a></li>
        <li><a class=${state.c[2]}  href="/filter/koala">koalas</a></li>
        <li><a class=${state.c[3]}  href="/filter/lion">lions</a></li>
        <li><a class=${state.c[4]}  href="/filter/tiger">tigers</a></li>
        <li><a class=${state.c[5]}  href="/filter/walrus">walruses</a></li>
      </ul>
    </div>
  </div>'

// map function
function animalMap (obj, i) {
return animal(remove, obj, i)
}

// add new animal to state
function add (e) {
var x = e.offsetX - 20
var y = e.offsetY - 10

emit('addAnimal', {x: x, y: y})
}

// map function
function animalMap (obj, i) {
var type = state.params.type

if (type && type !== obj.type) {
return // nothing
} else {
return animal(remove, obj, i)
}
}

// remove animal from state
function remove (e) {
var index = e.target.id
emit('removeAnimal', index)
}
}

sample app click behavior is weird

for first choo app the clicking behavior seems odd, once you click on animal type , you have to randomly get a click on that animal, wouldn't it be better once having choosen an animal to now only click on that animal.
change index.js , see **** new line commented code
index.js:
// import choo
var choo = require('choo')

// import choo's template helper
var html = require('choo/html')

// import template
var main = require('./templates/main.js')

// initialize choo
var app = choo()

app.use(function (state, emitter) {
// initialize state
state.animals = [
{ type: 'lion', x: 200, y: 100 },
{ type: 'crocodile', x: 50, y: 300 }
]

state.atype='all'

// add animal
emitter.on('addAnimal', function (data) {
var animals = ['crocodile', 'koala', 'lion', 'tiger', 'walrus']
var type = Math.floor(Math.random() * 5)

if (state.params.type) //**** new line
type=animals.indexOf(state.params.type); //**** new line

var x = data.x
var y = data.y

var obj = { type: animals[type], x: x, y: y }
state.animals.push(obj)

emitter.emit('render')
})

// remove animal
emitter.on('removeAnimal', function (i) {
state.animals.splice(i, 1)
emitter.emit('render')
})
})

// declare routes
app.route('/', main)
app.route('/filter/:type', main)

// start app
app.mount('div')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.