Skip to content

Commit

Permalink
add the ability to create triangles using css tricks
Browse files Browse the repository at this point in the history
Summary:- modify ReactViewBackgroundDrawable.java to make each border a trapezoid
 - disable anti-alias to eliminate white spaces between borders
 - add examples to BorderExample.js (see last one)
Closes #5911

Differential Revision: D2953734

Pulled By: dmmiller

fb-gh-sync-id: dd103d80dec53ad35c9539ab1ceb93ef857feeb9
shipit-source-id: dd103d80dec53ad35c9539ab1ceb93ef857feeb9
  • Loading branch information
Huang Yu authored and facebook-github-bot-3 committed Feb 19, 2016
1 parent a91466f commit a6a4389
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
25 changes: 23 additions & 2 deletions Examples/UIExplorer/BorderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,22 @@ var styles = StyleSheet.create({
borderTopLeftRadius: 10,
borderBottomRightRadius: 20,
borderColor: 'black',
elevation: 10
}
elevation: 10,
},
border11: {
width: 0,
height: 0,
borderStyle: 'solid',
overflow: 'hidden',
borderTopWidth: 50,
borderRightWidth: 0,
borderBottomWidth: 50,
borderLeftWidth: 100,
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: 'transparent',
borderLeftColor: 'red',
},
});

exports.title = 'Border';
Expand Down Expand Up @@ -209,4 +223,11 @@ exports.examples = [
return <View style={[styles.box, styles.border10]} />;
}
},
{
title: 'CSS Trick - Triangle',
description: 'create a triangle by manipulating border colors and widths',
render() {
return <View style={[styles.border11]} />;
}
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private static enum BorderStyle {
private @Nullable PathEffect mPathEffectForBorderStyle;
private @Nullable Path mPathForBorderRadius;
private @Nullable Path mPathForBorderRadiusOutline;
private @Nullable Path mPathForBorder;
private @Nullable RectF mTempRectForBorderRadius;
private @Nullable RectF mTempRectForBorderRadiusOutline;
private boolean mNeedUpdatePathForBorderRadius = false;
Expand Down Expand Up @@ -343,30 +344,64 @@ private void drawRectangularBackgroundWithBorders(Canvas canvas) {
int width = getBounds().width();
int height = getBounds().height();

// If the path drawn previously is of the same color,
// there would be a slight white space between borders
// with anti-alias set to true.
// Therefore we need to disable anti-alias, and
// after drawing is done, we will re-enable it.

mPaint.setAntiAlias(false);

if (mPathForBorder == null) {
mPathForBorder = new Path();
}

if (borderLeft > 0 && colorLeft != Color.TRANSPARENT) {
mPaint.setColor(colorLeft);
canvas.drawRect(0, borderTop, borderLeft, height - borderBottom, mPaint);
mPathForBorder.reset();
mPathForBorder.moveTo(0, 0);
mPathForBorder.lineTo(borderLeft, borderTop);
mPathForBorder.lineTo(borderLeft, height - borderBottom);
mPathForBorder.lineTo(0, height);
mPathForBorder.lineTo(0, 0);
canvas.drawPath(mPathForBorder, mPaint);
}

if (borderTop > 0 && colorTop != Color.TRANSPARENT) {
mPaint.setColor(colorTop);
canvas.drawRect(0, 0, width, borderTop, mPaint);
mPathForBorder.reset();
mPathForBorder.moveTo(0, 0);
mPathForBorder.lineTo(borderLeft, borderTop);
mPathForBorder.lineTo(width - borderRight, borderTop);
mPathForBorder.lineTo(width, 0);
mPathForBorder.lineTo(0, 0);
canvas.drawPath(mPathForBorder, mPaint);
}

if (borderRight > 0 && colorRight != Color.TRANSPARENT) {
mPaint.setColor(colorRight);
canvas.drawRect(
width - borderRight,
borderTop,
width,
height - borderBottom,
mPaint);
mPathForBorder.reset();
mPathForBorder.moveTo(width, 0);
mPathForBorder.lineTo(width, height);
mPathForBorder.lineTo(width - borderRight, height - borderBottom);
mPathForBorder.lineTo(width - borderRight, borderTop);
mPathForBorder.lineTo(width, 0);
canvas.drawPath(mPathForBorder, mPaint);
}

if (borderBottom > 0 && colorBottom != Color.TRANSPARENT) {
mPaint.setColor(colorBottom);
canvas.drawRect(0, height - borderBottom, width, height, mPaint);
mPathForBorder.reset();
mPathForBorder.moveTo(0, height);
mPathForBorder.lineTo(width, height);
mPathForBorder.lineTo(width - borderRight, height - borderBottom);
mPathForBorder.lineTo(borderLeft, height - borderBottom);
mPathForBorder.lineTo(0, height);
canvas.drawPath(mPathForBorder, mPaint);
}

// re-enable anti alias
mPaint.setAntiAlias(true);
}
}

Expand Down

0 comments on commit a6a4389

Please sign in to comment.