使用JavaScript将ID添加到画布

时间:2017-07-25 01:18:34

标签: javascript canvas

我正在尝试使用JavaScript向画布添加ID但是我正在尝试修改以下特定代码。请注意,我只是假设这是我设置属性的地方。欢呼任何帮助。

THREE.CanvasRenderer = function ( parameters ) {

console.log( 'THREE.CanvasRenderer', THREE.REVISION );

parameters = parameters || {};

var _this = this,
    _renderData, _elements, _lights,
    _projector = new THREE.Projector(),

    _canvas = parameters.canvas !== undefined
             ? parameters.canvas
             : document.createElement( 'canvas' ),

    _canvasWidth = _canvas.width,
    _canvasHeight = _canvas.height,
    _canvasWidthHalf = Math.floor( _canvasWidth / 2 ),
    _canvasHeightHalf = Math.floor( _canvasHeight / 2 ),

    _viewportX = 0,
    _viewportY = 0,
    _viewportWidth = _canvasWidth,
    _viewportHeight = _canvasHeight,

    _pixelRatio = 1,

    _context = _canvas.getContext( '2d', {
        alpha: parameters.alpha === true
    } ),

1 个答案:

答案 0 :(得分:3)

您可以直接设置id属性:

_canvas.id = 'whatever';

修改

上面的代码应该可以工作,但它需要在你拥有的大变量初始化块之后:

// start var initialization
var _this = this,
    _renderData, _elements, _lights,
    _projector = new THREE.Projector(),

    _canvas = parameters.canvas !== undefined
             ? parameters.canvas
             : document.createElement( 'canvas' ),

    _canvasWidth = _canvas.width,
    _canvasHeight = _canvas.height,
    _canvasWidthHalf = Math.floor( _canvasWidth / 2 ),
    _canvasHeightHalf = Math.floor( _canvasHeight / 2 ),

    _viewportX = 0,
    _viewportY = 0,
    _viewportWidth = _canvasWidth,
    _viewportHeight = _canvasHeight,

    _pixelRatio = 1,

    _context = _canvas.getContext( '2d', {
        alpha: parameters.alpha === true
    } ),

    ... more code,

    theEnd = true;
// end var initialization

_canvas.id = 'whatever';

^这整个事情是一个大的var初始化。在某些时候它会结束,可能会有一个分号。您将在其中添加ID代码。

我认为您收到了意外的令牌错误,因为您尝试在var初始化中的逗号之间添加一些代码。