Code Monkey home page Code Monkey logo

draft's Introduction

draft

About

draft is a simple drafting module for LÖVE 2D. It makes it easy to draft primitive shapes, and some more luxurious ones.

Loading the module

To load the module, use the following code.

-- modify the path depending where draft resides
local Draft = require('draft')
local draft = Draft(modeOption)

Setting mode is optional. The default is fill.

The drafting functions can then be called inside the love.draw() function.

Usage example

local Draft = require('draft')
local draft = Draft()

function love.draw()
  draft:rectangle(300, 100, 50, 30)
  draft:rectangle(500, 100, 50, 30)
  draft:rhombus(400, 200, 65, 65)
  draft:bow(390, 280, 100, 2.5, 0, 10, 'line')
end

Notes

  • The mode can be set when initializing the module, but it can also be overwritten with each drafting function. If you want to overwrite it for many functions, you can use the draft:getMode and draft:setMode functions.

  • You can also set the mode to false if you only want to get the vertices, and not draw the shape. For example, using the following code would get the vertices for a rectangle without drawing it.

    local vertices = draft:rectangle(100, 100, 50, 80, false)

  • Look at the file main.lua for examples on how the functions can be called. This file draws all the possible shapes.

  • The draft:compass function is a powerful function used to draw curves. Notably, it accepts a function for the scale parameter which permits the drawing of complex shapes. You can look at the code for draft:star and draft:egg for examples of usage. You can also look at the draft:compass function too see how it is called.

  • The linkers create line between points. Try them, they are quite powerful!

Draft function list

Initialization

draft(mode)

Getters and Setters

draft:getMode()
draft:setMode(mode)

Shapes

Primary Shapes (using core drawing functions)

draft:line(points, mode)
draft:triangleIsosceles(cx, cy, width, height, mode)
draft:triangleRight(cx, cy, width, height, mode)
draft:rectangle(cx, cy, width, height, mode)
draft:polygon(vertices, mode)

Secondary Shapes (using primary shape functions)

draft:triangleEquilateral(cx, cy, width, mode)
draft:square(cx, cy, length, mode)
draft:trapezoid(cx, cy, width, height, widthTop, widthTopOffset, mode)
draft:rhombus(cx, cy, width, height, mode)
draft:trapezium(cx, cy, widthLeft, widthRight, height, depth, mode)
draft:gem(cx, cy, widthTop, widthMiddle, height, depth, mode)
draft:diamond(cx, cy, width, mode)

Tertiary Shapes (using secondary shape functions)

draft:rhombusEquilateral(cx, cy, length, mode)
draft:lozenge(cx, cy, width, mode)
draft:kite(cx, cy, width, height, depth, mode)
draft:trapezoidIsosceles(cx, cy, width, height, widthTop, mode)
draft:parallelogram(cx, cy, width, height, widthOffset, mode)

Curved Shapes

draft:compass(cx, cy, width, arcAngle, startAngle, numSegments, wrap, scale, mode)
draft:circle(cx, cy, radius, numSegments, mode)
draft:arc(cx, cy, radius, arcAngle, startAngle, numSegments, mode)
draft:bow(cx, cy, radius, arcAngle, startAngle, numSegments, mode)
draft:pie(cx, cy, radius, arcAngle, startAngle, numSegments, mode)
draft:ellipse(cx, cy, width, height, numSegments, mode)
draft:ellipticArc(cx, cy, width, height, arcAngle, startAngle, numSegments, mode)
draft:ellipticBow(cx, cy, width, height, arcAngle, startAngle, numSegments, mode)
draft:ellipticPie(cx, cy, width, height, arcAngle, startAngle, numSegments, mode)
draft:semicircle(cx, cy, width, startAngle, numSegments, mode)
draft:dome(cx, cy, width, startAngle, numSegments, mode)

Complex Shapes

draft:star(cx, cy, width, widthInner, numPoints, startAngle, mode)
draft:egg(cx, cy, width, syBottom, syTop, numSegments, mode)

Linkers

draft:linkLadder(v1, v2, mode)
draft:linkTangle(v1, v2, mode)
draft:linkWeb(v, mode)
draft:linkTangleWebs(v1, v2, mode)

example_linker.lua (shows off linking)

-- load draft  
local Draft = require('draft')  
local draft = Draft()  

function love.load()  
  limitUpper = 100  
  limitLower = 4  
  numSegments = limitLower  
  direction = 'up'  
  step = 0.01  
end  

function love.update(dt)  
  if numSegments > limitUpper and direction == 'up' then  
    direction = 'down'  
  elseif numSegments < limitLower and direction == 'down' then  
    direction = 'up'
  elseif direction == 'up' then  
    numSegments = numSegments + step  
  else  
    numSegments = numSegments - step  
  end  
end  

function love.draw()  
  local v = draft:egg(400, 300, 1500, 1, 1, numSegments, 'line')  
  draft:linkWeb(v)  
end  

draft's People

Contributors

pelevesque avatar yonaba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

draft's Issues

Collision Question

Hi, i would like to know if there's some way i could use these shapes and have collisions with them. Collisions like point to shape, shape to shape, etc.

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.