简体   繁体   English

使用带3轴的鼠标Canvas绘制JavaScript

[英]JavaScript draw using mouse Canvas with 3 axis

How is it / is it possible to draw using the mouse a canvas using 3 axis(x,y,z). 如何使用鼠标绘制使用3轴(x,y,z)的画布。

I know that one can draw a canvas on 2 axis and I have done that successfully. 我知道可以在2轴上绘制画布,我已经成功完成了。

But I have no idea of how I shall draw it on 3 axis (for example a cube). 但我不知道如何在3轴(例如立方体)上绘制它。

Following shows some 2d canvas drawing functionallity 下面显示了一些2d画布绘图功能

$(canvas).on('mousemove', function(e) {
mousex = parseInt(e.clientX-canvasx);
mousey = parseInt(e.clientY-canvasy);
if(mousedown) {
    ctx.beginPath();
    if(tooltype=='draw') {
        ctx.globalCompositeOperation = 'source-over';
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 3;
    } else {
        ctx.globalCompositeOperation = 'destination-out';
        ctx.lineWidth = 10;
    }
    ctx.moveTo(last_mousex,last_mousey);
    ctx.lineTo(mousex,mousey);
    ctx.lineJoin = ctx.lineCap = 'round';
    ctx.stroke();
}
last_mousex = mousex;
last_mousey = mousey;
//Output
$('#output').html('current: '+mousex+', '+mousey+'<br/>last: '+last_mousex+', '+last_mousey+'<br/>mousedown: '+mousedown);

}); });

The full code https://jsfiddle.net/ArtBIT/kneDX/ . 完整代码https://jsfiddle.net/ArtBIT/kneDX/

But how can I add az axis and draw a 3d canvas for instance a cube. 但是如何添加az轴并绘制一个3d画布,例如一个立方体。

With 2D it is simple, you have the X and Y coordinate of the mouse, and when a mouse button is clicked you can change pixels at that location in the canvas. 使用2D很简单,您可以使用鼠标的X和Y坐标,单击鼠标按钮可以更改画布中该位置的像素。

3D on the other hand is quite hard. 另一方面3D非常难。 Because of the extra dimension that does not exist on the 2D surface, you need to know how to control the 3D positions. 由于2D表面上不存在额外的尺寸,您需要知道如何控制3D位置。 And to make matters worse, with that third dimension comes all kinds of extra's that everyone likes to have: lightning and shadows, effects, focus, etc. 更糟糕的是,第三个维度带来了每个人都喜欢的各种额外的东西:闪电和阴影,效果,焦点等。

Simple drawing 简单的绘图

In its most basic form, (set aside some arithmic) you can flatten the Z axis on the 2D surface with a single division. 在最基本的形式中,(预留一些算术)你可以用一个分区在2D表面上展平Z轴。 Suppose that you have a point in 3D which consists of three points on three axis (x3d, y3d, z3d) then you can do: 假设你有一个3D点,它包含三个轴上的三个点(x3d,y3d,z3d),那么你可以这样做:

var x2d = x3d / z3d;
var y2d = y3d / z3d;

If you're new to 3D, you will want to play with this first. 如果您是3D新手,您将首先想要使用它。 Here is a tutorial . 这是一个教程

Advanced drawing 高级绘图

For just particles and lines this is rather straightforward, although you might want to use another perspective . 对于粒子和线条,这是相当简单的,尽管您可能想要使用另一个透视图 But it gets more complicated soon when you use objects and want to rotate them in 3D space . 但是当你使用对象并希望在3D空间中旋转它们时,它会很快变得复杂。 This is why most people rely on an engine like three.js to do the 3D drawing for them. 这就是为什么大多数人依靠像three.js这样的引擎为他们做3D绘图的原因。

Control 3D space 控制3D空间

When drawing with the mouse, you need to map the 2D mouse movement to 3D for control. 使用鼠标绘图时,需要将2D鼠标移动映射到3D以进行控制。 For examples, have a look a these 3D GUI's: Microsoft's Paint 3D , Google's Sketchup , and Blender . 例如,看看这些3D GUI:微软的Paint 3D ,Google的SketchupBlender Note that the more kinds of mappings needs to be implemented (like scaling and other transformations) the more math is required. 请注意,需要实现的映射类型越多(如缩放和其他转换),则需要更多的数学运算

Using three.js would help you out. 使用three.js会帮助你。 See here: https://jsfiddle.net/bn890dtc/ 请看: https//jsfiddle.net/bn890dtc/

The core code for drawing the line as your click and drag: 在单击和拖动时绘制线条的核心代码:

function onMouseMove(evt) {
    if (renderer) {

        var x = (event.clientX / window.innerWidth) * 2 - 1;
        var y = -(event.clientY / window.innerHeight) * 2 + 1;
        var z = 0
        var vNow = new THREE.Vector3(x, y, z);

        vNow.unproject(camera);
        splineArray.push(vNow);

    }
}

The line 这条线

vNow.unproject(camera);

will project your drawing into 3D space. 将您的绘图投影到3D空间。

This function will update the line in 3D space: 此功能将更新3D空间中的线:

function updatePositions() {

    var positions = line.geometry.attributes.position.array;

    var index = 0;

        for ( var i = 0; i < splineArray.length;  i ++ ) {

        positions[ index ++ ] = splineArray[i].x;
        positions[ index ++ ] = splineArray[i].y;
        positions[ index ++ ] = splineArray[i].z;


        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM