F. 다크모드 구현
settingManager
를 통해 다크모드를 구현하는 예제입니다.
- 실행 결과
- 코드 보기
<html>
<head>
<script
src="https://www.mathcore.co.kr/api/libs/latest/helper.js"
defer
onload="onLoadHelper();"
></script>
<script>
var dark = window.matchMedia("(prefers-color-scheme: dark)").matches;
var backgroundColor, gridPrimaryColor, gridSecondaryColor, axisColor;
async function onLoadHelper() {
await cemware.helper.load("math2d", {
key: "TESTKEY"
});
const math2d = new cemware.math2d.Math2D();
math2d.render("math2d");
backgroundColor = [math2d.settingManager.background.color, "#000000"];
gridPrimaryColor = [math2d.settingManager.grid.primary.color, "#cccccc"];
gridSecondaryColor = [math2d.settingManager.grid.secondary.color, "#333333"];
axisColor = [math2d.settingManager.axis.color, "#ffffff"];
function updateTheme() {
math2d.settingManager.background.color = backgroundColor[Number(dark)];
math2d.settingManager.grid.primary.color = gridPrimaryColor[Number(dark)];
math2d.settingManager.grid.secondary.color = gridSecondaryColor[Number(dark)];
math2d.settingManager.axis.color = axisColor[Number(dark)];
}
document.getElementById("toggle-darkmode").addEventListener("click", () => {
dark = !dark;
updateTheme();
});
updateTheme();
}
</script>
</head>
<body>
<div id="math2d"></div>
<button id="toggle-darkmode">toggle darkmode</button>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
display: flex;
overflow: hidden;
}
#math2d {
position: relative;
flex: 1;
height: 100%;
}
#toggle-darkmode {
position: absolute;
right: 0;
top: 0;
padding: 20px;
}
</style>
</body>
</html>