使用html5画布创建6*6调色板
To create a 6 * 6 color palette using html5 canvas
我想使用HTML5 Canvas创建一个可点击的6*6调色板。我应该能够点击调色板上的每个颜色网格,背景颜色应该变为选定的颜色。
使用React,我做到了这一点。也许在看完这个之后,你会知道你需要做什么。
class Palette extends React.Component {
constructor(props) {
super(props);
this.state = { color: '#FFFFFF' };
}
setColor(row, col) {
const color = this.getColor(row, col);
this.setState({ color });
document.body.style.backgroundColor = color;
}
getColor(row, col) {
const hue = Math.floor(col / this.props.height * 360);
const sat = 100;
const lit = Math.floor((1 - (row + 1) / (this.props.width + 1)) * 100);
return `hsl(${hue},${sat}%,${lit}%)`;
}
render() {
const rows = [];
for (let i = 0; i < this.props.height; i++) {
const cols = [];
for (let j = 0; j < this.props.width; j++) {
cols.push(
<div
className="col"
onClick={
() => this.setColor(j, i)
}
style={{
backgroundColor: this.getColor(j, i)
}} />
);
}
rows.push(
<div className="row">
{cols}
</div>
);
}
return (
<div className="palette">
{rows}
</div>
);
}
}
ReactDOM.render(<Palette width={6} height={6} />, document.body);
.palette {
display: flex;
flex: 1;
flex-direction: column;
cursor: pointer;
}
.row {
display: flex;
flex: 1;
flex-direction: row;
}
.col {
width: 20px;
height: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 为effect Composer创建GodRays效果过程
- 从javascript创建一个列表
- onkeyup无法动态创建多个文本区域
- 如何使用javascript从主svg对象动态创建svg视图框
- 如何访问声音管理器2创建的声音对象
- 我已经创建了一个jquery转盘,并使用if条件来运行和停止转盘
- Kendo:我该如何在树视图中创建一个递归的hieiarchy
- 使用Facebook live API创建实时视频对象时的隐私设置
- 在动态创建的元素上获取对特定选择器的引用
- 如何创建带有插槽的vue js组件预加载程序
- 如何在创建键时引用来自同一对象的键
- 如何创建JSON数组
- 从html创建一个指令,该指令按类名应用函数
- 使用Javascript创建测验页面
- 更改使用Chart.js创建的图表中的轴线颜色
- JavaScript名称空间和对象创建
- JQuery对动态创建的对象进行选择
- 创建带有和不带有JavaScript的Bootstrap下拉菜单
- 使用html5画布创建6*6调色板