{"record":{"id":"dbf33dd6bbd54173","repo":"liabru/matter-js","slug":"mouseconstraint-create-options-mouse-was-undefine","errorCode":null,"errorMessage":"MouseConstraint.create: options.mouse was undefined, options.element was undefined, may not function as expected","messagePattern":"MouseConstraint\\.create: options\\.mouse was undefined, options\\.element was undefined, may not function as expected","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/constraint/MouseConstraint.js","lineNumber":45,"sourceCode":"     * Creates a new mouse constraint.\n     * All properties have default values, and many are pre-calculated automatically based on other properties.\n     * See the properties section below for detailed information on what you can pass via the `options` object.\n     * @method create\n     * @param {engine} engine\n     * @param {} options\n     * @return {MouseConstraint} A new MouseConstraint\n     */\n    MouseConstraint.create = function(engine, options) {\n        var mouse = (engine ? engine.mouse : null) || (options ? options.mouse : null);\n\n        if (!mouse) {\n            if (engine && engine.render && engine.render.canvas) {\n                mouse = Mouse.create(engine.render.canvas);\n            } else if (options && options.element) {\n                mouse = Mouse.create(options.element);\n            } else {\n                mouse = Mouse.create();\n                Common.warn('MouseConstraint.create: options.mouse was undefined, options.element was undefined, may not function as expected');\n            }\n        }\n\n        var constraint = Constraint.create({ \n            label: 'Mouse Constraint',\n            pointA: mouse.position,\n            pointB: { x: 0, y: 0 },\n            length: 0.01, \n            stiffness: 0.1,\n            angularStiffness: 1,\n            render: {\n                strokeStyle: '#90EE90',\n                lineWidth: 3\n            }\n        });\n\n        var defaults = {\n            type: 'mouseConstraint',","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/liabru/matter-js/blob/acb99b6f8784c809b940f1d2cf745427e088e088/src/constraint/MouseConstraint.js#L27-L63","documentation":"MouseConstraint.create was called with no engine.render.canvas and no options.element, so a Mouse could not be bound to any canvas. A default Mouse with no element is created and a warning is emitted; the constraint will exist but mouse interaction will not track pointer events correctly (mouse.position stays at origin).","triggerScenarios":"MouseConstraint.create({ mouse: undefined, element: undefined }) with no engine, or an engine created without Render (engine.render undefined), so neither fallback source for a canvas exists.","commonSituations":"Headless engine (Render.create never called) plus MouseConstraint; passing options in the wrong field (e.g. { canvas } instead of { element }); forgetting options entirely.","solutions":["Pass options.element: MouseConstraint.create(engine, { element: canvasElement }).","If using Matter.Render, pass the engine with an initialized render: ensure Render.create ran and engine.render.canvas exists.","Create the Mouse yourself: options.mouse = Mouse.create(canvas).","If truly headless, use your own input handling instead of MouseConstraint."],"exampleFix":"// before\nconst mc = MouseConstraint.create(engine); // no render, no element\n// after\nconst mc = MouseConstraint.create(engine, { element: document.querySelector('canvas') });","handlingStrategy":"validation","validationCode":"function canCreateMouseConstraint(engine, options) {\n  return Boolean(\n    (engine && engine.render && engine.render.canvas) ||\n    (options && options.element) ||\n    (options && options.mouse)\n  );\n}","typeGuard":"function hasMouseTarget(engine, options) {\n  return !!(options?.mouse || options?.element || engine?.render?.canvas);\n}","tryCatchPattern":null,"preventionTips":["Pass { element: canvas } explicitly when creating without a render","Initialize Render.create before MouseConstraint if relying on engine.render.canvas","Build Mouse yourself with Mouse.create(canvas) for custom setups","Remember MouseConstraint needs a DOM canvas; headless engines need custom input handling"],"tags":["input","mouse","configuration","misuse"],"backgroundTag":"missing-required-option","analyzedSha":"acb99b6f8784c809b940f1d2cf745427e088e088","analyzedAt":"2026-09-02T21:29:27.057Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}