为棋盘游戏响应地映射自定义形状的 HTML 元素的好方法 [英] Good way to map custom-shaped HTML elements responsively for a board game

查看:23
本文介绍了为棋盘游戏响应地映射自定义形状的 HTML 元素的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我还没有弄清楚如何以响应方式对齐空间.我试过使用 vw/vh's 和 %s,但两者都破坏了不同视图大小的一致性.任何人都可以推荐一种直接的方法来锁定这个板吗?

My problem is that I haven't figure out how to align the spaces responsively. I've tried using vw/vh's and %s but both break consistency on different view sizes. Can anyone recommend a straight-forward way to lock down this board?

我不想使用 50 多个空间的 cpu 消耗的多个画布 bc,也不想使用图像映射,因为它没有响应.

I didn't want to use multiple canvas bc of the cpu drain of 50+ spaces and don't want to use an image map because that's not responsive.

大家好,

我正在尝试通过 angular 1.x 将棋盘游戏移植到 HTML 中.我有一个包含每个板空间的对象,并通过绝对定位(顶部,左侧)为每个空间提供了一个位置原点,并创建了一个快速函数,该函数获取转换属性中的所有属性,并以 ng 样式返回用于 css 转换的字符串.

I'm trying to port a board game into HTML via angular 1.x. I have an object with each board space, and have given each space a location origin via absolute positioning (top, left) and made a quick function that takes all the properties in a transform property and returns a string for css transform in ng-style.

以下是前几个空格的示例:

Here's an example of the first few spaces:

1: {color: colors.blue, origin:{top:'65%', left:'13%'}, tForm: {rotate:'-21deg'}},
2: {color: colors.yellow, origin:{top:'66.5%', left:'8.5%'}, tForm: {rotate:'-35deg'}},
3: {color: colors.black, origin:{top:'70%', left:'6%'}, tForm: {rotate:'-72deg'}},
4: {color: colors.red, origin:{top:'75%', left:'5.5%'}, tForm: {rotate:'0deg'}},
5: {color: 'cool?', origin:{top:'80%', left:'6%'}, tForm: {rotate:'-21deg'}}

这是处理这些属性的代码(主要是 ng 风格):

Here's the code that's processing those properties (mostly in ng-style):

<span id="space-{{space}}" ng-repeat="space in section" ng-init="bgColor = cool.di.models.spaces[space].color === 'trap' ? '#900020' : cool.di.models.spaces[space].color === 'cool?' ? 'rgba(230,232,234, 1)': cool.di.models.spaces[space].color === 'pass' ? 'green' : cool.di.models.spaces[space].color" 
ng-style="{padding:'.1em', color:'mintcream', background:bgColor, border:'1px solid rgba(0,0,0,.2)', position:'absolute', top: cool.di.models.spaces[space].origin.top, left: cool.di.models.spaces[space].origin.left, transform: cool.di.api.tForm(cool.di.models.spaces[space].tForm), height:'2em', width:'2em'}">

这是我目前在C"上的进展https://irthos.github.io/酷/#/板

Here's my current progress on the "C" https://irthos.github.io/cool/#/board

感谢您的任何建议!

推荐答案

制作复杂形状集合的简单方法是使用带有多边形子元素的重复 SVG 元素.

What worked fairly simply to make the complex collection of shapes was a repeated SVG element with polygon subelement.

html:

<svg ng-style="{position:'absolute', background:'none', overflow: 'visible'}">
    <polygon points="{{points || '0,0 0,10 10,10 10,0 0,0'}}" stroke="black" fill="{{bgColor}}"></polygon>
 </svg>

其中 points 是从辅助函数生成的用于规范化视图尺寸的字符串:

Where points is a string generated from a helper function to normalize the view dimensions:

function(points){
    var pointsStr = '';
    points ?
    angular.forEach(points.split(' '), function (point) {
        var x = point.split(',')[0],
            y = point.split(',')[1];
        var hPx = Math.floor(($window.innerWidth / 100) * x),
            vPx = Math.floor(($window.innerHeight / 100) * y);
                pointsStr += hPx +','+vPx+' ';
            }) : null;
        return pointsStr.length > 0 ? pointsStr : null;
}

解析工厂对象中的poly"属性字符串:

Which parses the 'poly' property string in a factory object:

1: {color: colors.blue, poly:'13,5 8,5 10,11 12,11 13,5'},
2: {color: colors.yellow, poly:'8,5 4,8 7,13 10,11 8,5'},
3: {color: colors.black, poly:'4,8 1.8,13 5.5,16 7,13 4,8'},
4: {color: colors.red, poly:'1.8,13 0,19 5,19 5.5,16 1.8,13'},
5: {color: 'cool?', poly:'0,19 1,26 5.3,22.5 5,19 0,19'},
6: {color: colors.blue, poly: '1,26 3.5,31, 7,25 5.3,22.5 1,26'},
7: {color: colors.yellow, poly: '3.5,31 9,33 10,26 7,25 3.5,31'},
8: {color: colors.black, poly: '9,33 13,32 12,25.5 10,26 9,33'},
9: {color: 'cool?', poly:'13,32 18,28 15,24 12,25.5 13,32'}

全板渲染:https://irthos.github.io/cool/#/board

这篇关于为棋盘游戏响应地映射自定义形状的 HTML 元素的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆