【问题标题】:Passing variables into HSL color values in Three.JS在 Three.JS 中将变量传递给 HSL 颜色值
【发布时间】:2021-04-12 22:09:16
【问题描述】:

我目前在我的代码中设置了 2 种 HSL 颜色:

scene.background = new THREE.Color("hsl(0, 100%, 50%)");
var light = new THREE.AmbientLight("hsl(180, 100%, 50%)");

我正在尝试从我的 CSS 中为“hue”传递一个变量。

当我运行这段代码时,我可以看到我在 CSS 中定义的 2 个颜色变量,它们分别是 0 和 180:

var style = getComputedStyle(document.body);
console.log(style.getPropertyValue("--color-accent-hue")); // get accent hue color
console.log(style.getPropertyValue("--color-accent-hue-inverse")); // get accent hue inverse color

在我的代码开头,我将这些定义为 JS 变量,如下所示:

const colorAccentHue = style.getPropertyValue('--color-accent-hue');
const colorAccentHueInverse = style.getPropertyValue('--color-accent-hue-inverse');

然后尝试在我的init() 函数中调用它们,如下所示:

scene.background = new THREE.Color("hsl(colorAccentHue, 100%, 50%)");
var light = new THREE.AmbientLight("hsl(colorAccentHueInverse, 100%, 50%)");`

但我什么也得不到。我哪里错了?

【问题讨论】:

    标签: javascript colors three.js hsl


    【解决方案1】:

    您可以使用template literals(或字符串连接)。

    scene.background = new THREE.Color(`hsl(${colorAccentHue}, 100%, 50%)`);
    var light = new THREE.AmbientLight(`hsl(${colorAccentHueInverse}, 100%, 50%)`);
    

    【讨论】:

    • 完美。就是这样!
    • @ZekeWattles 乐于助人。
    猜你喜欢
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2010-10-12
    相关资源
    最近更新 更多