html城市绘制,HTML5/Canvas二分法构建城市版图

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

var ctx = fullscreenCanvas().ctx;

var canvas = ctx.canvas;

function Rect(pos, width, height) {

this.pos = pos;

this.width = width;

this.height = height;

}

Rect.prototype.update = Function.prototype;

Rect.prototype.draw = function() {

function rect(pos, width, height, depth) {

ctx.beginPath();

ctx.moveTo(pos.x, pos.y);

ctx.lineTo(pos.x + width, pos.y);

ctx.lineTo(pos.x + width, pos.y + height);

ctx.lineTo(pos.x, pos.y + height);

ctx.closePath();

ctx.stroke();

if (depth === 7) return;

var subdivisions = Choice.randint(2, 7);

var horizontal = Choice.chance(50);

if (depth === 7) {

subdivisions = 10;

}

for (let i = 0; i < subdivisions; i++) {

setTimeout(function() {

if (horizontal) {

rect(

Vector(pos.x,

pos.y + i * (height / subdivisions)),

width,

height / subdivisions,

depth + 1

);

} else {

// vertical.. duh

rect(

Vector(pos.x + i * (width / subdivisions),

pos.y),

width / subdivisions,

height,

depth + 1

);

}

}, 15 * i);

}

}

rect(this.pos, this.width, this.height, 0);

}

ctx.fillStyle = 'white';

ctx.fillRect(0, 0, canvas.width, canvas.height);

var entities = createEntityPool();

var r1 = new Rect(Vector(0, 0), canvas.width, canvas.height);

entities.add('rects', r1);

entities.drawGroup('rects');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是HTML5/Canvas交互式3D立方体的代码,你可以直接复制粘贴到HTML文件中进行实验: ```html <!DOCTYPE html> <html> <head> <style> canvas { border: 1px solid black; } </style> </head> <body> <canvas id="canvas" width="400" height="400"></canvas> <script> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); // 定义立方体的六个面 var cube = [ { x: -50, y: -50, z: -50 }, { x: -50, y: 50, z: -50 }, { x: 50, y: 50, z: -50 }, { x: 50, y: -50, z: -50 }, { x: -50, y: -50, z: 50 }, { x: -50, y: 50, z: 50 }, { x: 50, y: 50, z: 50 }, { x: 50, y: -50, z: 50 }, ]; // 定义立方体的六个面的连接方式 var connections = [ [0, 1, 2, 3], [4, 5, 6, 7], [0, 4, 5, 1], [1, 5, 6, 2], [2, 6, 7, 3], [3, 7, 4, 0], ]; // 定义立方体的初始位置和旋转角度 var position = { x: 200, y: 200, z: 0 }; var rotation = { x: 0, y: 0, z: 0 }; // 绘制立方体 function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.save(); ctx.translate(position.x, position.y); ctx.rotate(rotation.z); ctx.rotate(rotation.y); ctx.rotate(rotation.x); ctx.translate(-position.x, -position.y); for (var i = 0; i < connections.length; i++) { ctx.beginPath(); for (var j = 0; j < connections[i].length; j++) { var p = project(cube[connections[i][j]]); if (j == 0) { ctx.moveTo(p.x, p.y); } else { ctx.lineTo(p.x, p.y); } } ctx.closePath(); ctx.stroke(); } ctx.restore(); } // 投影函数,将3D坐标点投影到2D平面上 function project(p) { var scale = 200 / (200 + p.z + position.z); return { x: p.x * scale + position.x, y: p.y * scale + position.y, }; } // 鼠标控制旋转 var mousedown = false; var mouse = { x: 0, y: 0 }; canvas.addEventListener("mousedown", function (e) { mousedown = true; mouse.x = e.clientX; mouse.y = e.clientY; }); canvas.addEventListener("mouseup", function () { mousedown = false; }); canvas.addEventListener("mousemove", function (e) { if (mousedown) { rotation.x += (e.clientY - mouse.y) * 0.01; rotation.y += (e.clientX - mouse.x) * 0.01; mouse.x = e.clientX; mouse.y = e.clientY; draw(); } }); // 渲染循环 function loop() { requestAnimationFrame(loop); draw(); } loop(); </script> </body> </html> ``` 这个代码将会在canvas标签中绘制一个3D立方体,并且根据鼠标的控制进行旋转。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值