Certainly! Below is a JavaScript function that takes a Valorant crosshair code string as input and generates an SVG representation of the crosshair based on the parameters specified in the code.
function generateCrosshairSVG(crosshairCode) {
// Parse the crosshair code into parameters
const params = parseCrosshairCode(crosshairCode);
// SVG dimensions
const width = 500;
const height = 500;
const centerX = width / 2;
const centerY = height / 2;
// Initialize SVG elements array
let svgElements = [];
// Set up crosshair color
let color = '#FFFFFF'; // Default to white
if (params['u']) {
color = '#' + params['u'];
} else if (params['c']) {
// Map color index to hex color
const colorMap = {
'0': '#FFFFFF', // White
'1': '#00FF00', // Green
'2': '#0000FF', // Blue
'3': '#FFFF00', // Yellow
'4': '#FFA500', // Orange
'5': '#FF0000', // Red
'6': '#00FFFF', // Cyan
'7': '#FF00FF', // Pink
'8': '#000000' // Black
};
color = colorMap[params['c']] || '#FFFFFF';
}
// Draw center dot if enabled
if (parseInt(params['d']) === 1) {
const dotOpacity = parseFloat(params['a']) || 1;
const dotThickness = parseFloat(params['t']) || 2;
svgElements.push(`<circle cx="${centerX}" cy="${centerY}" r="${dotThickness}" fill="${color}" opacity="${dotOpacity}"/>`);
}
// Function to draw lines
function drawLines(type) {
const enabledKey = `${type}b`;
if (parseInt(params[enabledKey]) === 1) {
const thickness = parseFloat(params[`${type}t`]) || 2;
const length = parseFloat(params[`${type}l`]) || 10;
const offset = parseFloat(params[`${type}v`]) || 0;
const opacity = parseFloat(params[`${type}o`]);
const lineOpacity = isNaN(opacity) ? 1 : opacity / 10;
// Horizontal lines
svgElements.push(`<rect x="${centerX - length - offset}" y="${centerY - thickness / 2}" width="${length}" height="${thickness}" fill="${color}" opacity="${lineOpacity}"/>`); // Left
svgElements.push(`<rect x="${centerX + offset}" y="${centerY - thickness / 2}" width="${length}" height="${thickness}" fill="${color}" opacity="${lineOpacity}"/>`); // Right
// Vertical lines
svgElements.push(`<rect x="${centerX - thickness / 2}" y="${centerY - length - offset}" width="${thickness}" height="${length}" fill="${color}" opacity="${lineOpacity}"/>`); // Top
svgElements.push(`<rect x="${centerX - thickness / 2}" y="${centerY + offset}" width="${thickness}" height="${length}" fill="${color}" opacity="${lineOpacity}"/>`); // Bottom
}
}
// Draw inner lines (type '0')
drawLines('0');
// Draw outer lines (type '1')
drawLines('1');
// Generate SVG content
const svgContent = `
<svg width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">
${svgElements.join('\n')}
</svg>
`;
return svgContent.trim();
}
function parseCrosshairCode(code) {
const tokens = code.split(';');
const params = {};
let key = null;
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (isNaN(parseFloat(token)) || token === '') {
key = token;
} else if (key !== null) {
params[key] = token;
key = null;
}
}
return params;
}
// Example usage:
const crosshairCode = "0;P;c;8;b;1;t;1;o;0.5;z;2;a;1;0t;2;0l;7;0v;7;0o;0;0a;1;0s;0.5;0e;1;1t;2;1l;2;1v;2;1o;10;1a;0.35;1s;1;1e;1;u;FFFFFF;d;1;h;0;0g;0;1g;0;0f;1;1f;1;0m;1;1m;1;0b;1;1b;0;m;0";
const svg = generateCrosshairSVG(crosshairCode);
// Display the SVG (in a browser environment)
document.body.innerHTML = svg;
Explanation:
-
Parsing the Crosshair Code:
- The
parseCrosshairCode function splits the input string by semicolons and iterates over the tokens.
- It identifies parameter keys and values, storing them in an object for easy access.
-
Setting Up SVG Parameters:
- The SVG canvas is set to 500x500 pixels, with the center at (250, 250).
- The crosshair color is determined by either the custom color
u or the color index c, mapped to hex codes.
-
Drawing Crosshair Components:
- Center Dot:
- If enabled (parameter
d), a circle SVG element is created at the center with the specified thickness and opacity.
- Inner and Outer Lines:
- The
drawLines helper function handles drawing both inner ('0') and outer ('1') lines based on their respective parameters.
- It creates rectangles for each line (top, bottom, left, right) with specified length, thickness, offset, and opacity.
-
Generating the SVG:
- All SVG elements are combined into a single SVG string.
- The SVG is returned by the
generateCrosshairSVG function.
-
Usage:
- Replace the
crosshairCode variable with any valid Valorant crosshair code string.
- The generated SVG can be displayed in a web page by setting
document.body.innerHTML = svg; or used elsewhere as needed.
Notes:
- The function focuses on the primary elements of the crosshair: center dot, inner lines, and outer lines.
- Some parameters from the crosshair code may not be used if they don't correspond to visual elements (e.g., error multipliers).
- Adjustments can be made to handle additional parameters or to refine the rendering based on more detailed mappings of the crosshair code.
Example Output:
The above code will render an SVG crosshair based on the provided crosshair code. You can embed this SVG in an HTML page or manipulate it further as needed.
Disclaimer:
This is a basic implementation and may not cover all possible parameters of the Valorant crosshair code. For a complete and accurate rendering, you may need to expand the parameter mappings and handling based on the game's specifications.