Skip to content

Instantly share code, notes, and snippets.

@naosim
Created February 28, 2016 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naosim/35f24c287e6c2aff8fb6 to your computer and use it in GitHub Desktop.
Save naosim/35f24c287e6c2aff8fb6 to your computer and use it in GitHub Desktop.
テキスト画像作成
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>メニューテキストメーカー</title>
<style>
@import url(https://fonts.googleapis.com/css?family=Lobster);
html, body, h2 {
padding: 0px;
margin: 0px;
}
.root {
}
.text {
font-family: Lobster;
z-index: 1;
position: absolute;
padding: 0px 12px 0px 12px;
margin: 0px;
text-align: center;
vertical-align: middle;
height: 40px;
line-height: 40px;
box-sizing: border-box;
border-bottom-style: solid;
border-bottom-width: : 2px;
}
.edit {
padding-top: 100px;
}
</style>
</head>
<body>
<div class="root">
<div class="text"></div>
</div>
<div class="edit">
<h2>編集</h2>
文字 :<input id="textInput" value="GAME" /><br>
下線色:<input id="colorInput" value="#f00" /><br>
<button id="apply">反映</button><br>
<a href="" id="menu" download="menu.png">画像を保存</a>
</div>
<script src="http://naosim.sakura.ne.jp/app/texteditsample/html2canvas.js"></script>
<script>
var textBox = document.querySelector('.text');
var getTextWidth = function() {
return textBox.offsetWidth;
};
var applyWidth = function() {
// 編集値適用
textBox.innerHTML = textInput.value;
textBox.style['border-bottom-color'] = colorInput.value;
// 画像作成
html2canvas(document.querySelector('.root'),{
onrendered: function(canvas){
var data = canvas.toDataURL("image/png");
document.getElementById("menu").href = data;
},
width: getTextWidth(),
height: 40
});
};
document.querySelector('#apply').addEventListener('click', applyWidth);
window.onload = function() {
applyWidth();
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment