简体   繁体   English

使用JavaScript向画布添加ID

[英]Add an ID to a canvas using JavaScript

I'm trying to add an ID to a canvas using JavaScript however I'm trying to modify the following specific code. 我正在尝试使用JavaScript向画布添加ID,但是我试图修改以下特定代码。 Please note, I am only assuming that this is where I would set the attribute. 请注意,我仅假设这是设置属性的地方。 Cheers for any help. 为任何帮助加油。

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
    } ),

You can just set the id property directly: 您可以直接设置id属性:

_canvas.id = 'whatever';

Edit 编辑

The above code should work, but it will need to be after the large variable initialization block you have: 上面的代码应该可以工作,但是需要在大型变量初始化块之后:

// 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';

^ this whole thing is one big var initialization. ^这整个事情是一个很大的var初始化。 At some point there it will end and there will probably be a semicolon. 在某个时候它将结束,并且可能会有分号。 That's where you will add the id code. 那是您将添加ID代码的地方。

I think you were getting an unexpected token error because you were trying to add some code between the commas in the var initialization. 我认为您遇到了意外的令牌错误,因为您试图在var初始化的逗号之间添加一些代码。

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

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