Skip to content

Instantly share code, notes, and snippets.

@nexpr
nexpr / StylitElement.js
Last active October 24, 2023 01:50
LitElement で直接 CSS を書けるようにする
import { LitElement } from "lit"
export class StylitElement extends LitElement {
constructor() {
super()
this._cssss = new CSSStyleSheet()
this._style_num = 0
}
style(template, ...values) {
const class_name = "c" + (this._style_num++).toString(36)
<!doctype html>
<meta charset="utf-8" />
<script type="module">
const addLog = (key) => {
const div = document.createElement("div")
div.textContent = `key: ${key}`
setTimeout(() => {
div.animate([
{ height: div.clientHeight + "px" },
@nexpr
nexpr / index.js
Created August 19, 2023 09:22
create dummy images using node.js
const fs = require("fs")
const { createCanvas } = require("canvas")
const alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const chars = alphabets + alphabets.toLowerCase() + "1234567890"
for (const [index, char] of [...chars].entries()) {
const canvas = createCanvas(80, 80)
const ctx = canvas.getContext("2d")
ctx.font = "bold 70px serif"
@nexpr
nexpr / 1.js
Created August 18, 2023 12:33
ceil, round, floor 関数
export const ceil = (n, d) => Math.ceil(n * (10 ** d)) / (10 ** d)
export const round = (n, d) => Math.round(n * (10 ** d)) / (10 ** d)
export const floor = (n, d) => Math.floor(n * (10 ** d)) / (10 ** d)
import { useState, useContext, createContext } from "react"
const CounterContext = createContext()
const CounterProvider = CounterContext.Provider
const useCounter = () => useContext(CounterContext)
const useNewCounter = () => {
const [count, setCount] = useState(0)
return {
@nexpr
nexpr / measure.js
Created October 8, 2022 06:25
複数の関数の実行時間計測
export default (fns, measure_count, effective_rate) => {
const data = []
for (let i = 0; i < measure_count; i++) {
for (const [fn_index, fn] of fns.entries()) {
if (!data[fn_index]) {
data[fn_index] = []
}
const start = performance.now()
  • XML としてパースする
  • S タグの属性でスタイルを指定
    • a: text-align
    • c: color
    • d: text-decoration
    • f: font-family
    • s: font-size
    • w: font-weight
    • ex: <S w="bold" c="blue">blue-bold</S>
  • D タグでタグ定義
<!doctype html>
<meta charset="utf-8" />
<script type="module">
import { html, css, LitElement, createRef, ref } from "https://cdn.jsdelivr.net/gh/lit/dist@2.2.8/all/lit-all.min.js"
import "https://unpkg.com/@fluentui/web-components"
const common_styles = css`
.btns {
display: flex;
@nexpr
nexpr / import-script-module.js
Last active August 14, 2022 10:00
同じ HTML ファイル中の script タグを import 可能にする
{
const name_to_url = {}
for (const script of document.querySelectorAll("script[data-module]")) {
name_to_url[script.dataset.module] = URL.createObjectURL(new Blob([script.innerHTML], { type: "text/javascript" }))
}
const importmap = document.createElement("script")
importmap.type = "importmap"
importmap.innerHTML = `{"imports": ${JSON.stringify(name_to_url)}}`
document.head.append(importmap)
@nexpr
nexpr / lit-lib-example.html
Created August 6, 2022 17:04
lit-html でルートごとにイベントを伝える
<!DOCTYPE html>
<meta charset="utf-8" />
<script type="module">
import { html, render } from "https://cdn.skypack.dev/lit-html"
const lit = (root, template, init, actions) => {
let state = init
const update = async (fn, ...args) => {