i'd like a javascript funciton that returns an svg element. so instead of this const…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
i'd like a javascript funciton that returns an svg element. so instead of this const line = document.createElementNS(xmlns, "line"); line.setAttribute("x1", x1); line.setAttribute("y1", y1); line.setAttribute("x2", x2); line.setAttribute("y2", y2); line.setAttribute("stroke", stroke); line.setAttribute("stroke-width", strokeWidth);
you could do this:
codeconst line = createSVGElem({ type: 'line', attributes: { "x1": x1, "y1": y1, "x2": x2, "y2": y2, "stroke": stroke, "stroke-width": strokeWidth }});which should be more elegant and readable
- Assistant
Certainly! Here's the updated function with the requested changes:
javascriptfunction createSVGElem(type, attributes) { const xmlns = "http://www.w3.org/2000/svg"; const elem = document.createElementNS(xmlns, type); if (attributes) { for (const [key, value] of Object.entries(attributes)) { elem.setAttribute(key, value); } } return elem; } // Usage example: const line = createSVGElem('line', { x1: x1, y1: y1, x2: x2, y2: y2, stroke: stroke, 'stroke-width': strokeWidth });Now the function accepts a type and a single attributes object, and it checks if attributes exist before setting them.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.