Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div>
  <input type="range" min="1.05" max="30" step="0.25" value="8" class="value-a" />
  方眼幅: <span class="value-a">8</span> px
</div>
<div class="wrapper">
  <svg width="500" height="500"
       viewBox="0 0 500 500" class="container">
  </svg>
</div>

<div>
  <img src="" class="tone">
</div>
              
            
!

CSS

              
                html {
  margin: 0;
  padding: 8px;
  background-color: #e9e7e4;
}

img {
  width: 500px;
  height: 500px;
}

div.wrapper {
  visibility: hidden;
  width: 0;
  height: 0;
}

svg {
  background-color: #fff;
}
              
            
!

JS

              
                const ns = 'http://www.w3.org/2000/svg'

const getCenterPosition = (x0, y0, a) => {
  return [
    x0 + (a * Math.random()),
    y0 + (a * Math.random())
  ]
}

const dotRadius = 1 // ドットの半径
const svgSize = 500 // パターンキャンバスの一辺長
// 区画の一辺の長さを受け取る
const drawDots = a => {
  const svgRoot = document.querySelector('svg.container')
  svgRoot.innerHTML = ''
  const width = svgRoot.clientWidth
  const num = Math.ceil(width / a)
  
  // デバッグ用の方眼
  // for (let r = 0; r < num; r++) {
  //   const x = r * a
  //   for (let c = 0; c < num; c++) {
  //     const y = c * a
  //     const rect = document.createElementNS(ns, 'rect')
  //     rect.setAttribute('width', a)
  //     rect.setAttribute('height', a)
  //     rect.setAttribute('x', r * a)
  //     rect.setAttribute('y', c * a)
  //     rect.setAttribute('fill', 'transparent')
  //     rect.setAttribute('stroke', '#ccc')
  //     rect.setAttribute('stroke-width', 1)
  //     svgRoot.appendChild(rect)
  //   }
  // }
  
  // 方眼内のランダムな位置に中心を持つドット
  for (let r = 0; r < num; r++) {
    const x = r * a
    for (let c = 0; c < num; c++) {
      const y = c * a
      const [cx, cy] = getCenterPosition(x, y, a)
      const circle = document.createElementNS(ns, 'circle')
      circle.setAttribute('fill', '#000')
      circle.setAttribute('r', dotRadius)
      circle.setAttribute('cx', cx)
      circle.setAttribute('cy', cy)
      svgRoot.appendChild(circle)
    }
  }
}

// PNG画像化する
const generatePngImage = () => {
  const svgRoot = document.querySelector('svg.container')
  const img = document.querySelector('img.tone')
  const svgStr = btoa(unescape(encodeURIComponent(
    new XMLSerializer().serializeToString(svgRoot))))

  console.log("new XMLSerializer().serializeToString(svgRoot):")
  console.log(new XMLSerializer().serializeToString(svgRoot))
  console.log("svgStr:")
  console.log(svgStr)
  
  const srcUrl = `data:image/svg+xml;charset=utf-8;base64,${svgStr}`
  const scale = 1 /// (dotRadius * 2)
  const image = new Image()
  image.onload = function () {
    const canvas = document.createElement('canvas')
    // DPRを考慮した一辺長
    const size = svgSize //* window.devicePixelRatio * scale
    canvas.width = size
    canvas.height = size
    const ctx = canvas.getContext('2d')
    ctx.fillStyle = '#fff'
    ctx.fillRect(0, 0, size, size)
    ctx.drawImage(image, 0, 0, svgSize, svgSize, 0, 0, size, size)
    img.src = canvas.toDataURL('image/png')
  }
  image.src = srcUrl
}

const main = a => {
  drawDots(a)
  generatePngImage()
}

window.addEventListener('load', () => { 
  const range = document.querySelector('input.value-a')
  range.addEventListener('change', e => {
    const value = +e.target.value
    document.querySelector('span.value-a').innerText = value
    main(value)
  }, false)
  
  main(8)
}, false)
              
            
!
999px

Console