New buildings, trees, and math functions
Overview
index.html
: Added scripts forbuilding.js
andtree.js
.js/items/building.js
: New classBuilding
for drawing buildings.js/items/tree.js
: New classTree
for drawing trees.js/math/graph.js
: Added static methodload()
for loading graph info.js/math/utils.js
: Added functionslerp2D()
,getFake3dPoint()
, andgetRandomColor()
.js/primitives/polygon.js
: Addedjoin
option todraw()
method.js/world.js
: Changed representation of buildings, added trees, changed how they are drawn and added aviewPoint
parameter to thedraw(ctx, viewPoint)
method.
File wise changes made
index.html
- Added the scripts
js/items/tree.js
andjs/items/building.js
in the<head>
tag - Removed
world.draw(ctx);
and added the following code:- Created a variable
viewPoint
- Added
world.draw(ctx, viewPoint);
- Added
ctx.globalAlpha = 0.3;
- Added
graphEditor.display();
- Added
requestAnimationFrame(animate);
- Created a variable
js/items/building.js
- Added a new class called
Building
which takes a polygon and a height as parameters Building
has adraw()
method that draws the building in the context provided- The
draw()
method creates the base, the sides, the ceiling, the roof, and the top midpoints of the building - It then draws the base, the sides, the ceiling, and the roof of the building
js/items/tree.js
- Added a new class called
Tree
which takes a center, a size, and a height as parameters Tree
has adraw()
method that draws the tree in the context provided- The
draw()
method generates a base polygon for the tree, and then draws it - It then creates a top point for the tree, and draws the trunk of the tree
- It then draws the leaves of the tree using a loop that creates a series of polygons and draws them
js/math/graph.js
- Added a static method called "load" which takes a Graph object as a parameter
- The "load" method creates a new Graph object with the points and segments from the JSON object
- It then returns the new Graph object
- Added a "hash" method to the Graph class which returns a JSON string representation of the Graph object
js/math/utils.js
- Added a function called
lerp2D
which takes two points and a value between 0 and 1 as parameters - The
lerp2D
function returns a new point which is the linear interpolation between the two points, with the value t specifying the weighting between the two points - Added a function called
getFake3dPoint
which takes a point, a view point, and a height as parameters - The
getFake3dPoint
function returns a new point which is the 3D projection of the point onto the plane defined by the view point and the height
js/primitives/polygon.js
- Added a
join
parameter to thedraw
method of the Polygon class - The
join
parameter specifies the type of join to be used when drawing the polygon
js/world.js
- Changed
...this.buildings.map((b) => b.points).flat()
to...this.buildings.map((b) => b.base.points).flat()
in the#generateTrees()
method - Added
viewPoint
as a parameter to thedraw
method of the World class - Changed
for (const tree of this.trees)
toitems.sort(...)
andfor (const item of items)
in thedraw
method of the World class - Changed
tree.draw(ctx, { size: this.treeSize, color: "rgba(0,0,0,0.5)" });
toitem.draw(ctx, viewPoint);
in thedraw
method of the World class - Changed
for (const bld of this.buildings) { bld.draw(ctx); }
tofor (const bld of this.buildings) { bld.draw(ctx); }
in thedraw
method of the World class
Summary
- index.html:
- Added