3d drawing in Canvas with HTML+JS

Is there any framework/engine that provide ability to draw 3d images on Canvas? I am planning to draw some primitives (different shapes) located in one plane: var dist = 2; var hexHalfW = 35; var lengthX = 20; var hexR = Math.sqrt(lengthX*lengthX+hexHalfW*hexHalfW);//40.31128874 var hexDiag = 2*hexR; var hexHeight = hexDiag; var hexWidth = 2*hexHalfW; function DrawHex(ctx, x, y) { var cx = x*(hexWidth + dist) - y*(hexWidth + dist)/2; var cy = y*(hexR + lengthX + dist); ctx.beginPath(); ctx.moveTo(cx, cy-hexR); ctx.lineTo(cx+hexHalfW, cy-hexR+lengthX); ctx.lineTo(cx+hexHalfW, cy+hexR-lengthX); ctx.lineTo(cx, cy+hexR); ctx.lineTo(cx-hexHalfW, cy+hexR-lengthX); ctx.lineTo(cx-hexHalfW, cy-hexR+lengthX); ctx.lineTo(cx, cy-hexR); ctx.fill(); } function draw() { var canvas = document.getElementById('tutorial'); if (canvas.getContext) { var ctx = canvas.getContext('2d'); //Pick Hexagon color, this one will be blue ctx.fillStyle = "rgb(0, 0, 255)"; DrawHex(ctx, 1, 1); ctx.fillStyle = "rgb(0, 0, 225)"; DrawHex(ctx, 3, 1); ctx.fillStyle = "rgb(0, 0, 195)"; DrawHex(ctx, 4, 1); ctx.fillStyle = "rgb(0, 0, 165)"; DrawHex(ctx, 6, 1); ctx.fillStyle = "rgb(0, 255, 0)"; DrawHex(ctx, 3, 2); ctx.fillStyle = "rgb(0, 225, 0)"; DrawHex(ctx, 4, 2); ctx.fillStyle = "rgb(0, 195, 0)"; DrawHex(ctx, 5, 2); } } I would like to draw these primitives in "perspective": closer shapes (in the bottom of the screen) will be bigger, shapes "far away" (in the top of the screen) needs to be smaller... For this purpose shapes coordinates are needs to be recalculated. Guess, I could find some formulas that allows to recalculate coordinates and write a proper functions... but, probably, there are some engines that already doing that? Please advise. Any thoughts are welcome!

以上就是3d drawing in Canvas with HTML+JS的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏