下面的javascript代码应该根据用户输入生成背景图像。 用户应该能够更改条纹的角度和深度,颜色等其他内容... 问题是当角度改变时,我无法让图案无缝重复。 我认为这是因为该图案始终从同一点开始。 有没有办法控制它?
var atts = {
size: [30, 30],
depth: 50,
rotDegrees: 45
};
function refresh() {
var code = $('#tpl').html().replace(/{\:([^{}]+)}/g, function(match, key) {
return atts[key] || match;
});
code = '<svg xmlns="http://www.w3.org/2000/svg" width="' + atts.size[0] + '" height="' + atts.size[1] + '">' + code + '</svg>';
$('#preview').css('background-image', 'url("data:image/svg+xml;base64,' + window.btoa(code) + '")');
};
refresh();
$('input').on('input', function(e) {
atts[this.name] = this.value;
refresh();
});
#preview {
width: 600px;
height: 600px;
display: block;
background: transparent none repeat center center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="rotDegrees" type="range" min="0" max="360" step="1" value="0" />
<input name="depth" type="range" min="1" max="100" step="1" value="60" />
<div id="preview"></div>
<script id="tpl" type="text/template"></script>